在我的程序中,我使用SimpleAdapter显示从SD卡到listview的图像,但我得到OutofMemory和File太大的错误,所以我试图将图像缩放到低尺寸,因为我创建了一个单独的类文件来执行此操作< / p>
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;
}
}
我将这个位图直接返回到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将缩放后的图像保存到给定位置而没有任何问题,我只有在将位图返回到主类中的hashmap时才会出现此错误。 / p>
注意:我也可以将字符串格式的直接路径提供给主图像并且它可以正常工作,但如果图像太大,则会出现错误。 hashmap_temp.put( “图像”,img_path_new + “/” + cursor_receipt.getString(7)+ “JPG”);
答案 0 :(得分:0)
SimpleAdapter
只能绑定int(资源id)或字符串。
bindView
的{{1}}实施使用对象的ImageView
作为图片的路径。
你可以做的是将拇指uri toString
放在地图而不是位图中(同样,它将允许一次加载最少数量的位图,而不是所有位图)。
将方法签名更改为
file_uri+"THUMB1_"+file_name+".jpg"
并返回:
public String convertIntoThumb(String file_uri,String file_name)