我正在制作一个类似于Android手机上的“通讯录”应用的目录应用,其中包含此人的照片及其名称。但是,我需要从互联网上获取这些图像作为长字符串并将其转换为图像。
在向联系人列表中向上和向下滚动几次后,我收到了“java.lang.OutOfMemoryError:位图大小超过VM预算”。
下面的代码中是否有明显的东西我应该做的不同?
将图像作为字符串下载并存储在asynctask中:
//Image doesn't exist, need to add
try {
FileOutputStream fOut = (activity.getApplicationContext()).openFileOutput(guid, 0);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(strImage);
osw.flush();
osw.close();
contact.setImageLength(String.valueOf(strImage.length()));
db.updateContact(contact);
} catch (IOException ioe) {
ioe.printStackTrace();
}
在列表适配器中显示图像:
case R.layout.listview_member_row:
LayoutInflater inflater4 = ((Activity)context).getLayoutInflater();
row = inflater4.inflate(layoutResourceId, parent, false);
holder = new PositionHolder();
holder.txtName = (TextView)row.findViewById(R.id.listview_member_name);
holder.imgPicture = (ImageView)row.findViewById(R.id.listview_picture);
try {
FileInputStream fIn = context.openFileInput(contact.getContactId());
InputStreamReader isr = new InputStreamReader(fIn);
char[] inputBuffer = new char[Integer.parseInt(contact.getImageLength())];
isr.read(inputBuffer);
String encodedImage = new String(inputBuffer);
// Convert string to image
byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
// Assign image to screen
holder.imgPicture.setImageBitmap(decodedByte);
} catch (IOException oe) {
oe.printStackTrace();
}
row.setTag(holder);
holder.txtName.setText(listItem.name);
holder.txtName.setTextSize(nameTextSize);
break;