Ich versuche, eine Bitmap oder ein Drawable aus dem vorhandenen Dateipfad zu erstellen.
String path = intent.getStringExtra("FilePath");
BitmapFactory.Options option = new BitmapFactory.Options();
option.inPreferredConfig = Bitmap.Config.ARGB_8888;
mImg.setImageBitmap(BitmapFactory.decodeFile(path));
// mImg.setImageBitmap(BitmapFactory.decodeFile(path, option));
// mImg.setImageDrawable(Drawable.createFromPath(path));
mImg.setVisibility(View.VISIBLE);
mText.setText(path);
Aber setImageBitmap()
, setImageDrawable()
zeigt kein Bild vom Pfad. Ich habe den Pfad mit mText
gedruckt und es sieht so aus: /storage/sdcard0/DCIM/100LGDSC/CAM00001.jpg
Was mache ich falsch? Kann mir jeder helfen?
Bitmap aus Dateipfad erstellen:
File sd = Environment.getExternalStorageDirectory();
File image = new File(sd+filePath, imageName);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
imageView.setImageBitmap(bitmap);
Wenn Sie die Bitmap auf Höhe und Breite des übergeordneten Objekts skalieren möchten, verwenden Sie die Funktion Bitmap.createScaledBitmap
.
Ich denke, Sie geben den falschen Dateipfad an. :) Hoffe das hilft.
Für mich geht das:
File imgFile = new File("/sdcard/Images/test_image.jpg");
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
//Drawable d = new BitmapDrawable(getResources(), myBitmap);
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
}
Bearbeiten:
Wenn das oben festcodierte SD-Kartenverzeichnis in Ihrem Fall nicht funktioniert, können Sie den SD-Kartenpfad abrufen:
String sdcardPath = Environment.getExternalStorageDirectory().toString();
File imgFile = new File(sdcardPath);
hier ist eine Lösung:
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
Nun, die Verwendung der statischen Drawable.createFromPath(String pathName)
scheint mir etwas unkomplizierter zu sein, als sie selbst zu dekodieren ... :-)
Wenn Ihre mImg
eine einfache ImageView
ist, brauchen Sie sie nicht einmal. Verwenden Sie mImg.setImageUri(Uri uri)
direkt.
static ArrayList< Drawable> d;
d = new ArrayList<Drawable>();
for(int i=0;i<MainActivity.FilePathStrings1.size();i++) {
myDrawable = Drawable.createFromPath(MainActivity.FilePathStrings1.get(i));
d.add(myDrawable);
}
sie können nicht über einen Pfad auf Ihre Drawables zugreifen. Wenn Sie also eine von Menschen lesbare Schnittstelle mit Ihren Drawables wünschen, können Sie diese programmgesteuert erstellen.
deklarieren Sie irgendwo in Ihrer Klasse eine HashMap:
private static HashMap<String, Integer> images = null;
//Then initialize it in your constructor:
public myClass() {
if (images == null) {
images = new HashMap<String, Integer>();
images.put("Human1Arm", R.drawable.human_one_arm);
// for all your images - don't worry, this is really fast and will only happen once
}
}
Jetzt zum Zugang -
String drawable = "wrench";
// fill in this value however you want, but in the end you want Human1Arm etc
// access is fast and easy:
Bitmap wrench = BitmapFactory.decodeResource(getResources(), images.get(drawable));
canvas.drawColor(Color .BLACK);
Log.d("OLOLOLO",Integer.toString(wrench.getHeight()));
canvas.drawBitmap(wrench, left, top, null);