【发布时间】:2011-09-05 08:20:57
【问题描述】:
我在 android 中有一个应用程序,其中我正在处理非常大的图像(640x480) 并且稍微大一点。这实际上是用相机拍摄的照片,然后被编辑,然后保存到 sdcard 最后上传到服务器。
但我面临的问题是VM memory exceeded 与bitmaps 合作时。
我正在做这样的事情:
在第一个活动中,我收到了the bytes from the camera 并创建了一个bitmap,它经过编辑然后保存到sdcard
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
options.inDither = true;
byte[] imageData = extras.getByteArray("imageData");
myImage = BitmapFactory.decodeByteArray(imageData, 0,
imageData.length, options);
Matrix mat = new Matrix();
mat.postRotate(90);
if(myImage !=null){
bitmapResult = Bitmap.createBitmap(myImage.get(), 0, 0, (myImage.get()).getWidth(),
(myImage.get()).getHeight(), mat, true);
在onPause() 方法中我这样做了:
bitmapResult.recycle();
bitmapResult=null;
在我的第二个活动中,我从sdcard 读取文件并将其显示在屏幕上。为什么?我有我的理由:
File f=new File("/sdcard/Images/android.jpg");
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inTempStorage = new byte[16*1024];
o2.inSampleSize=8;
o2.outWidth=100;
o2.outHeight=100;
try {
x = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(x != null){
myImage.setImageBitmap(x);
}
在onPause()做了同样的事情
x.recycle();
x=null;
这一切都没有奏效,拍了几张照片后我的应用程序崩溃了。
我尝试使用WeaakReference 而不是bitmap:
myImage = new WeakReference<Bitmap>(BitmapFactory.decodeByteArray(imageData, 0,
imageData.length, options));
仍然没有....同样的错误....out of memory.
有人知道吗?
P.S:我也尝试拨打System.gc(),但结果相同!
所以请帮帮我!
【问题讨论】:
-
全局声明所有变量/对象。
-
他们在每个活动中都是全球性的:)...这就是他们的方式
-
Variables/Objects 表示 Bitmap、Drawable 等,并在活动的最后部分设置 null。
-
标签: android bitmap weak-references