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?
Well, using the static
Drawable.createFromPath(String pathName)
seems a bit more straightforward to me than decoding it yourself... :-)If your
mImg
is a simpleImageView
, you don't even need it, usemImg.setImageUri(Uri uri)
directly.It works for me:
Edit:
If above hard-coded sdcard directory is not working in your case, you can fetch the sdcard path:
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:
Now for access -
here is a solution:
Create bitmap from file path:
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. :) Hope this helps.