javaandroidandroid-drawablebitmapfactory

Create a Bitmap/Drawable from file path


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?


Solution

  • 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.