Android回收位图错误

时间:2015-05-08 04:46:13

标签: android canvas bitmap imageview recycle

我正在进行控制以调整位图大小并插入到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();
        }
    }
}

为什么我遇到回收错误?

2 个答案:

答案 0 :(得分:2)

要解决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;

}

答案 1 :(得分:0)

使用以下代码行。

SELECT u.user_id
     , u.name
  FROM users u
 WHERE NOT EXISTS ( SELECT 1 
                      FROM friends f
                     WHERE f.user_id = u.user_id
                       AND f.status = 2
                       AND f.friend_id = ?
                  )
   AND NOT EXISTS ( SELECT 1
                      FROM friends g
                     WHERE g.friend_id = u.user_id
                       AND g.status = 2
                       AND g.user_id = ?
                  )
   AND u.user_id <> ?