【问题标题】:Reading image from custom application image path in Android从Android中的自定义应用程序图像路径读取图像
【发布时间】:2013-10-08 05:21:53
【问题描述】:

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

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

【问题讨论】:

    标签: android image storage external


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      BitmapFactory.decodeFile(path);
      

      【讨论】:

        【解决方案3】:

        其他答案是正确的,但您可能会得到一个OutOfMemoryError 用于high 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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多