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

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

于 2013-10-08T05:32:08.867 に答える