Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 234
Nari Kim Shin Avatar asked May 29 '13 02:05

Nari Kim Shin


People also ask

How do I make a drawable bitmap?

This example demonstrates how do I convert Drawable to a Bitmap in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do you create a bitmap image in Java?

To create a bitmap from a resource, you use the BitmapFactory method decodeResource(): Bitmap bitmap = BitmapFactory. decodeResource(getResources(), R. drawable.

How do I get bitmap from imageView?

Bitmap bm=((BitmapDrawable)imageView. getDrawable()). getBitmap(); Try having the image in all drawable qualities folders (drawable-hdpi/drawable-ldpi etc.)


1 Answers

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. :) Hope this helps.

like image 126
CodeShadow Avatar answered Oct 12 '22 22:10

CodeShadow