0

如何从以下路径读取图像作为位图?谢谢你。

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

3 回答 3

1

应该使用 ==>BitmapFactory.decodeFile(pathName)方法。如果外部存储中的文件在清单中声明权限

于 2013-10-08T05:23:55.780 回答
1

在 BitmapFactory 中使用此方法,它将返回一个位图对象..

BitmapFactory.decodeFile(path);
于 2013-10-08T05:24:12.997 回答
0

其他答案是正确的,但可能你会得到一个图像OutOfMemoryErrorhigh resolution比如相机图片的图像。所以为避免这种情况,您可以使用以下功能

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

看到这个错误https://stackoverflow.com/a/13226946/942224

于 2013-10-08T05:32:08.867 回答