Creating a Bitmap / Drawable from a File Path

I am trying to create a Bitmap or Drawable from an 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() does not display the image from the path. I printed the path using mText and it looks like this: /storage/sdcard0/DCIM/100LGDSC/CAM00001.jpg

What am I doing wrong? Can anybody help me?

+50
android bitmapfactory android-drawable
May 29 '13 at 2:09
source share
6 answers

Create a bitmap from the 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 height and width of the parent, use the Bitmap.createScaledBitmap function.

I think you are setting the wrong file path. :) Hope this helps.

+79
Feb 05 '15 at 18:58
source share

This 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 the sdcard hard drive above does not work in your case, you can get the path to the SDK:

 String sdcardPath = Environment.getExternalStorageDirectory().toString(); File imgFile = new File(sdcardPath); 
+47
May 29 '13 at 2:17
source share

here is the solution:

 Bitmap bitmap = BitmapFactory.decodeFile(filePath); 
+16
Apr 7 '16 at 22:40
source share

Well, using static Drawable.createFromPath(String pathName) seems a little easier for me than decoding it ... Drawable.createFromPath(String pathName)

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

+3
Jan 16 '15 at 22:03
source share
 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); } 
+1
Apr 17 '15 at 7:09
source share

you cannot access your drawings through the path, so if you want the user interface with your drawings to be readable.

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); 
0
Aug 30 '14 at 17:14
source share



All Articles