我希望獲取存儲在SD卡上的圖像的寬度和高度(以像素爲單位),然後將它們加載到RAM中。我需要知道尺寸,所以我可以在加載它們時對它們進行縮減採樣。如果不下采樣他們,我會得到一個OutOfMemoryException。android:在不打開它的情況下獲取圖像尺寸
任何人都知道如何獲取圖像文件的尺寸?
我希望獲取存儲在SD卡上的圖像的寬度和高度(以像素爲單位),然後將它們加載到RAM中。我需要知道尺寸,所以我可以在加載它們時對它們進行縮減採樣。如果不下采樣他們,我會得到一個OutOfMemoryException。android:在不打開它的情況下獲取圖像尺寸
任何人都知道如何獲取圖像文件的尺寸?
傳遞到範圍只是解碼工廠的選項:
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;
HTH
其實,還有另一種方式來解決這個問題。使用下面的方式,我們可以避免文件和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);
另請參閱http://stackoverflow.com/questions/23867823/get-image-width-and-height-from-uri?lq=1。 – CoolMind
如何獲取密度值? – breceivemail
那工作時options.inJustDecodeBounds = false; – Cabezas
返回0的高度和寬度 – AlwaysConfused