Android 缩放位图后将其返回到hashmap时,无法解码流错误
在我的程序中,我使用SimpleAdapter将图像从SD卡显示到listview,但由于内存不足,文件太大,所以我尝试将图像缩放到较低的大小,为此我创建了一个单独的类文件 ThumbCreator.classAndroid 缩放位图后将其返回到hashmap时,无法解码流错误,android,bitmap,filenotfoundexception,Android,Bitmap,Filenotfoundexception,在我的程序中,我使用SimpleAdapter将图像从SD卡显示到listview,但由于内存不足,文件太大,所以我尝试将图像缩放到较低的大小,为此我创建了一个单独的类文件 ThumbCreator.class public Bitmap convertIntoThumb(String file_uri,String file_name) { try { File f = new File(file_uri, file_name+".jpg"); Bitmap
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;
}
}
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);
我将此位图直接返回到hashmap.put()方法中的主活动类,如下所示
Main.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;
}
}
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);
注意:在解码图像时,我没有收到此错误,因为它使用FileoutputStream将缩放图像保存到给定位置时没有任何问题。只有在将位图返回到main类中的hashmap时,我才会收到此错误
注意:我也可以给主图像提供字符串格式的直接路径,它可以工作,但如果图像太大(如下图所示),则会出现错误。
hashmap_temp.put(“image”,img_path_new+“/”+cursor_receive.getString(7)+.jpg”)
simpledapter
只能绑定int(资源id)或字符串
ImageView
的bindView
实现使用对象的toString
作为图像的路径
您可以做的是将thumb urifile_uri+“THUMB1\u”+file_name+”.jpg“
放在地图中而不是位图中(同时,它将允许一次加载最少数量的位图,而不是全部位图)
将方法签名更改为
public String convertIntoThumb(String file_uri,String file_name)
并返回:
return file_uri+"THUMB1_"+file_name+".jpg";
但它将返回原始图像,对吗?我需要在“位图”对象中获得的缩放图像。您在
位图中拥有的是您在fOutStream中写入的内容。因此,您在这个文件中拥有的是您想要的缩放位图。哦,对不起,我弄错了,我得到了它,我会尝试它是否工作,但实际上我不需要保存缩放图像,因为它会增加sd卡上的内存,我写fOutStream只是为了检查BitmapFactory.decodeStream(新文件输入流(f),null,o2);给出了这个错误,有没有办法在不保存的情况下显示缩放图像?另一个选择是实现一个自定义适配器来处理位图对象。我发现很难使用自定义适配器我是Android编程新手,所以,现在你的回答帮了我很大的忙,非常感谢。