51

RAMにロードする前に、SDカードに保存されている画像の幅と高さ(ピクセル単位)を取得したい。サイズを知る必要があるため、ロード時にそれに応じてダウンサンプリングできます。それらをダウンサンプリングしないと、OutOfMemoryException が発生します。

画像ファイルのサイズを取得する方法を知っている人はいますか?

4

2 に答える 2

127

境界をデコードするだけのオプションをファクトリに渡します。

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

//Returns null, sizes are in the options variable
BitmapFactory.decodeFile("/sdcard/image.png", options);
int width = options.outWidth;
int height = options.outHeight;
//If you want, the MIME type will also be decoded (if possible)
String type = options.outMimeType;
于 2012-06-20T21:30:53.957 に答える
2

実は、この問題を解決する別の方法があります。以下の方法を使用すると、ファイルと URI に関する問題を回避できます。

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
ParcelFileDescriptor fd = mContext.getContentResolver().openFileDescriptor(u, "r"); // u is your Uri
BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, options);
于 2015-10-05T02:43:02.430 に答える