[英]Load big Images in Android
眾所周知,Android在將位圖數據存儲在RAM內存中存在問題。 我需要在視圖上加載圖像(來自13Mpx相機的照片),然后才能放大和縮小圖像。 圖像應該是可變的。 現在,它是通過以下方式實現的:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
_bitmap = BitmapFactory.decodeFile(_path, options);
當我拍一張大照片(13或8 Mpx)時,programm被壓縮,出現“內存不足”錯誤。 我需要一些解決此問題的方法。 我需要一些可以加載和操作(縮放)大圖像的類。 它必須等於或小於API-8。
我嘗試了Universall Image Loader,但沒有縮放選項。 有人知道如何解決此問題的想法嗎?
一個位圖每個像素占用4個字節==> 13MP等於52MB的內存。 您應該使用BitmapFactory.Options僅首先獲取大小。 通過使用inJustDecodeBounds
您將獲得具有所有元數據但沒有實際圖像的Bitmap對象。
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
然后計算屏幕所需的比例尺大小:
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
並使用它加載位圖的縮放版本:
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
《 開發人員指南》中對此都有詳細說明
試試這個縮放位圖
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
/*
* If set to a value > 1, requests the decoder to
* subsample the original image, returning a smaller
* image to save memory. The sample size is the
* number of pixels in either dimension that
* correspond to a single pixel in the decoded
* bitmap. For example, inSampleSize == 4 returns an
* image that is 1/4 the width/height of the
* original, and 1/16 the number of pixels. Any
* value <= 1 is treated the same as 1. Note: the
* decoder uses a final value based on powers of 2,
* any other value will be rounded down to the
* nearest power of 2.
*/
options.inSampleSize = 2;
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(path, options);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;
為避免java.lang.OutOfMemory
異常,請在解碼位圖之前檢查位圖的尺寸,除非您完全相信源可為您提供可預測大小的圖像數據,以適合可用內存。 參考
非常感謝大家為您提供的所有答案。
我以為我應該使用inJustDecodeBounds選項並加載調整大小的位圖,但是對於有經驗的人來說,這個問題需要重新審視。
我還考慮過使用Universal Image Loader加載和緩存大圖像,以及使用ScaleImageView進行縮放。
有什么可以解決我的問題的。 部分加載位圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.