Create a Bitmap/Drawable from file path
Asked Answered
C

7

99

I'm trying to create a Bitmap or Drawable from existing file path.

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);

But setImageBitmap(), setImageDrawable() doesn't show an image from the path. I've printed path with mText and it looks like : /storage/sdcard0/DCIM/100LGDSC/CAM00001.jpg

What am i doing wrong? Anyone can help me?

Cavetto answered 29/5, 2013 at 2:9 Comment(4)
BitmapFactory.decodeFile(path) --> does this return an Bitmap object for you ? can you verify it ?Raiment
@autobot_101 in debug mode, it has id in mBuffer. But its mHeight, mWidth value is -1, and mLayoutBounds is null.Cavetto
Then you should check your file path again, because that means your image has not been 'inflated' to the bitmap object. Maybe you can try another imageRaiment
@autobot_101 actually i got this image path from Cursor and tried other images, but same result. Also, i checked the path via adb shell and found out images are exists in that path.Cavetto
R
160

Create bitmap from file path:

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);

If you want to scale the bitmap to the parent's height and width then use Bitmap.createScaledBitmap function.

I think you are giving the wrong file path.

Rosati answered 5/2, 2015 at 18:58 Comment(3)
I already got my solution long time ago, but I'll take this as a correct answer because it can also handles OOM errors while you're loading a big scale image. Very clean and nice solution! Thanks!Cavetto
I assume imageName here is any string? or is it any specific imageName?Locally
@JeetendraChoudhary Yes imageName could be any final String as the name of the image.Rosati
D
71

It works for me:

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);

}

Edit:

If above hard-coded sdcard directory is not working in your case, you can fetch the sdcard path:

String sdcardPath = Environment.getExternalStorageDirectory().toString();
File imgFile = new  File(sdcardPath);
Deherrera answered 29/5, 2013 at 2:17 Comment(1)
Try getting SdCard path from Environment.getExternalStorageDirectory().toString() and then tryCabezon
P
51

here is a solution:

Bitmap bitmap = BitmapFactory.decodeFile(filePath);
Preciosity answered 7/4, 2016 at 22:40 Comment(1)
I was searching for it!Aecium
M
3

Well, using the static Drawable.createFromPath(String pathName) seems a bit more straightforward to me than decoding it yourself... :-)

If your mImg is a simple ImageView, you don't even need it, use mImg.setImageUri(Uri uri) directly.

Monteux answered 16/1, 2015 at 22:3 Comment(0)
I
2
For Drawable -
Drawable drawable = Drawable.createFromPath(your path in string);
For Bitmap -
Bitmap bitmap = BitmapFactory.decodeFile(your path in string);

How simple it was hope you like

Intine answered 3/4, 2021 at 14:9 Comment(0)
R
1
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);
}
Rarefied answered 17/4, 2015 at 7:9 Comment(1)
Note that you should explain the code that you provide in an answer.Ebneter
C
0

you can't access your drawables via a path, so if you want a human readable interface with your drawables that you can build programatically.

declare a HashMap somewhere in your class:

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
  }
}

Now for access -

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);
Cyanite answered 30/8, 2014 at 17:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.