Nizzy
Nizzy

Reputation: 1889

Reading image from custom application image path in Android

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";

Upvotes: 0

Views: 1339

Answers (3)

Sanket Kachhela
Sanket Kachhela

Reputation: 10856

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

Upvotes: 0

kalyan pvs
kalyan pvs

Reputation: 14590

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

BitmapFactory.decodeFile(path);

Upvotes: 1

nurisezgin
nurisezgin

Reputation: 1570

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

Upvotes: 1

Related Questions