Android 回收位图错误
Android recycled bitmap error
我要调整位图的大小并插入到 imageView 中。
但是我遇到了回收错误...
错误信息:
05-08 13:32:48.948: E/AndroidRuntime(4792):
Process: com.test.myapp, PID: 4792
05-08 13:32:48.948: E/AndroidRuntime(4792):
java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@2f1aa6ad
05-08 13:32:48.948: E/AndroidRuntime(4792): at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1225)
05-08 13:32:48.948: E/AndroidRuntime(4792): at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:600)
05-08 13:32:48.948: E/AndroidRuntime(4792): at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:544)
05-08 13:32:48.948: E/AndroidRuntime(4792): at android.widget.ImageView.onDraw(ImageView.java:1187)
05-08 13:32:48.948: E/AndroidRuntime(4792): at android.view.View.draw(View.java:16209)
请问如何解决我的情况..
我的代码:
Bitmap photo = BitmapFactory.decodeFile(full_path);
SaveBitmapToFileCache(photo, Environment.getExternalStorageDirectory().getPath() + "/test/"+filename);
imgview.setImageBitmap(photo);
private Bitmap getBitmapSize(Bitmap photo){
ByteArrayOutputStream os = new ByteArrayOutputStream();
Bitmap bmp = photo;
Bitmap resizeBmp = null;
if(bmp.getWidth() > 2000 || bmp.getHeight() > 2000)
resizeBmp = resizingBitmap(mContext, bmp, bmp.getWidth()/10 , bmp.getHeight()/10, false);
else if(bmp.getWidth() > 1000 || bmp.getHeight() > 1000)
resizeBmp = resizingBitmap(mContext, bmp, bmp.getWidth()/5 , bmp.getHeight()/5, false);
else
resizeBmp = resizingBitmap(mContext, bmp, bmp.getWidth() , bmp.getHeight(), false);
resizeBmp.compress(CompressFormat.PNG, 90, os);
bmp.recycle();
return resizeBmp;
}
private void SaveBitmapToFileCache(Bitmap bitmap, String strFilePath) {
File fileCacheItem = new File(strFilePath);
OutputStream out = null;
try{
fileCacheItem.createNewFile();
out = new FileOutputStream(fileCacheItem);
bitmap.compress(CompressFormat.JPEG, 100, out);
}
catch (Exception e) {
e.printStackTrace();
}
finally{
try{
out.close();
}
catch (IOException e){
e.printStackTrace();
}
}
}
为什么我遇到回收错误?
使用这些代码行..
if(resizeBmp !=null)
{
resizeBmp .recycle();
bmp.recycle();
bmp=null;
resizeBmp =null;
}
要修复 OutOfMemory 错误,您应该这样做:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap = BitmapFactory.decodeStream(is, null, options);
这个 inSampleSize 选项减少了内存消耗。
这是一个完整的方法。首先它读取图像大小而不解码内容本身。然后它找到最好的inSampleSize值,应该是2的幂,最后解码图像。
// Decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
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_SIZE=70;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while(o.outWidth / scale / 2 >= REQUIRED_SIZE &&
o.outHeight / scale / 2 >= REQUIRED_SIZE) {
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;
}
我要调整位图的大小并插入到 imageView 中。
但是我遇到了回收错误...
错误信息:
05-08 13:32:48.948: E/AndroidRuntime(4792):
Process: com.test.myapp, PID: 4792
05-08 13:32:48.948: E/AndroidRuntime(4792):
java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@2f1aa6ad
05-08 13:32:48.948: E/AndroidRuntime(4792): at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1225)
05-08 13:32:48.948: E/AndroidRuntime(4792): at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:600)
05-08 13:32:48.948: E/AndroidRuntime(4792): at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:544)
05-08 13:32:48.948: E/AndroidRuntime(4792): at android.widget.ImageView.onDraw(ImageView.java:1187)
05-08 13:32:48.948: E/AndroidRuntime(4792): at android.view.View.draw(View.java:16209)
请问如何解决我的情况..
我的代码:
Bitmap photo = BitmapFactory.decodeFile(full_path);
SaveBitmapToFileCache(photo, Environment.getExternalStorageDirectory().getPath() + "/test/"+filename);
imgview.setImageBitmap(photo);
private Bitmap getBitmapSize(Bitmap photo){
ByteArrayOutputStream os = new ByteArrayOutputStream();
Bitmap bmp = photo;
Bitmap resizeBmp = null;
if(bmp.getWidth() > 2000 || bmp.getHeight() > 2000)
resizeBmp = resizingBitmap(mContext, bmp, bmp.getWidth()/10 , bmp.getHeight()/10, false);
else if(bmp.getWidth() > 1000 || bmp.getHeight() > 1000)
resizeBmp = resizingBitmap(mContext, bmp, bmp.getWidth()/5 , bmp.getHeight()/5, false);
else
resizeBmp = resizingBitmap(mContext, bmp, bmp.getWidth() , bmp.getHeight(), false);
resizeBmp.compress(CompressFormat.PNG, 90, os);
bmp.recycle();
return resizeBmp;
}
private void SaveBitmapToFileCache(Bitmap bitmap, String strFilePath) {
File fileCacheItem = new File(strFilePath);
OutputStream out = null;
try{
fileCacheItem.createNewFile();
out = new FileOutputStream(fileCacheItem);
bitmap.compress(CompressFormat.JPEG, 100, out);
}
catch (Exception e) {
e.printStackTrace();
}
finally{
try{
out.close();
}
catch (IOException e){
e.printStackTrace();
}
}
}
为什么我遇到回收错误?
使用这些代码行..
if(resizeBmp !=null)
{
resizeBmp .recycle();
bmp.recycle();
bmp=null;
resizeBmp =null;
}
要修复 OutOfMemory 错误,您应该这样做:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap = BitmapFactory.decodeStream(is, null, options);
这个 inSampleSize 选项减少了内存消耗。
这是一个完整的方法。首先它读取图像大小而不解码内容本身。然后它找到最好的inSampleSize值,应该是2的幂,最后解码图像。
// Decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
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_SIZE=70;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while(o.outWidth / scale / 2 >= REQUIRED_SIZE &&
o.outHeight / scale / 2 >= REQUIRED_SIZE) {
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;
}