0

How can I read an image from the following paths as a bitmap? Thank you.

String path = "file:///storage/emulated/0/Pictures/MY_FILES/1371983157229.jpg";
String path = "file:///storage/sdcard0/Android/data/com.dropbox.android/files/scratch/Camera%20Uploads/2045.12.png";
Nizzy
  • 1,879
  • 3
  • 21
  • 33

3 Answers3

1

should be use ==> BitmapFactory.decodeFile(pathName) method. If file in external stroge declare permission in manifest

nurisezgin
  • 1,530
  • 12
  • 19
1

Use this method in BitmapFactory it will return you a bitmap object..

BitmapFactory.decodeFile(path);
kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
0

other answer is correct but may be you will get a OutOfMemoryError for high resolution images like images of camera pictures. so to avoid this you can use below function

   public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){
        try {
            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);

            //The new size we want to scale to
            final int REQUIRED_WIDTH=WIDTH;
            final int REQUIRED_HIGHT=HIGHT;
            //Find the correct scale value. It should be the power of 2.
            int scale=1;
            while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
                scale*=2;

            //Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        }
            catch (FileNotFoundException e) {}
        return null;
    }

see this for that error https://stackoverflow.com/a/13226946/942224

Community
  • 1
  • 1
Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75