当我 return 位图缩放到散列图后无法解码流错误

Unable to decode stream Error when i return bitmap to hashmap after scaling it

在我的程序中,我使用 SimpleAdapter 将 SD 卡中的图像显示到列表视图,但我遇到了 OutofMemory 和 File too large 错误,所以我试图将图像缩放到较小的尺寸,为此我创建了一个单独的 class 文件来执行此操作

ThumbCreator.class

public Bitmap convertIntoThumb(String file_uri,String file_name)
{
    try
    {

    File f = new File(file_uri, file_name+".jpg");

    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;

    Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);

    FileOutputStream fOutStream = new FileOutputStream(new File(file_uri+"THUMB1_"+file_name+".jpg"));
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOutStream);

    fOutStream.flush();
    fOutStream.close();

    return bitmap;
}


catch (FileNotFoundException e) 
{
    // TODO: handle exception
    return null;
}
catch (IOException e) 
{
    // TODO: handle exception
    return null;
}

}

我return这个位图到我的主要activityclass在hashmap.put()方法直接如下

Main.class

    final ArrayList<HashMap<String, ?>> list = new ArrayList<HashMap<String,?>>();


    //***adding receipts from particular record to cursor.
    cursor = sqldb.rawQuery("select * from table_1 where record_name = '"+record_name+"'",null);
    curso.moveToFirst();

    while (curso.isAfterLast()!=true) 
    {
        HashMap<String, Object> hashmap_temp = new HashMap<String,Object>();

        hashmap_temp.put("name", cursor_receipt.getString(1));


        if(cursor.getString(7).toString() != "")
        {
            hashmap_temp.put("image",thumbcreator.convertIntoThumb(img_path_new,cursor_receipt.getString(7)));

        }

        list.add(hashmap_temp);

        cursor.moveToNext();
    }
    cursor.close();

    SimpleAdapter adptr_list;

    adptr_list = new SimpleAdapter(context_receipt, list, R.layout.activity_textview_receipt, new String[]{"image","name"},new int[]{R.id.img_receipt,R.id.tv_name});

    lv = (ListView)findViewById(R.id.lv);
    lv.setAdapter(adptr_list);

我得到的错误是

01-28 16:51:28.147: E/BitmapFactory(5911): Unable to decode stream: java.io.FileNotFoundException: /android.graphics.Bitmap@b3e145e0: open failed: ENOENT (No such file or directory)
01-28 16:51:28.157: I/System.out(5911): resolveUri failed on bad bitmap uri: android.graphics.Bitmap@b3e145e0
01-28 16:51:28.407: E/BitmapFactory(5911): Unable to decode stream: java.io.FileNotFoundException: /android.graphics.Bitmap@b3e31950: open failed: ENOENT (No such file or directory)
01-28 16:51:28.477: I/System.out(5911): resolveUri failed on bad bitmap uri: android.graphics.Bitmap@b3e31950

注意:我在解码图像时没有收到此错误,因为它使用 FileoutputStream 将缩放后的图像保存到给定位置没有任何问题,只有当我 return 位图到主要 class.

中的哈希图

注意:我还可以以字符串格式提供主图像的直接路径,它可以工作,但如果图像太大,则会出现错误,如下所示。 hashmap_temp.put("image",img_path_new+"/"+cursor_receipt.getString(7)+".jpg");

SimpleAdapter只能绑定int(资源id)或字符串。

ImageViewbindView 实现使用对象的 toString 作为图像的路径。

您可以做的是将缩略图 uri file_uri+"THUMB1_"+file_name+".jpg" 放在地图中而不是位图中(另外,它允许一次加载最少数量的位图,而不是全部)。

将您的方法签名更改为

public String convertIntoThumb(String file_uri,String file_name)

和return:

return file_uri+"THUMB1_"+file_name+".jpg";