在我的应用程序中,我正在使用 AsyncTask 从服务器下载图像。当我将 base64 转换为位图时,我遇到了异常。我在网上搜索过,但没有解决我的问题。请帮助我。我的代码是:
try{
JSONArray jArray = new JSONArray(result_resp);
for(int i=0;i<jArray.length();i++) {
JSONObject jobject = jArray.getJSONObject(i);
String originalitemid = jobject.getString("OriginItemId");
String resType = jobject.getString("ItemType");
String response = jobject.getString("ResImage");
String resid = jobject.getString("ResId");
byte[] decodedByte = null;
try {
decodedByte = Base64.decode(response, 0);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap bm = BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
System.out.println("the bitmap value is:"+bm);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] data = baos.toByteArray();
try {
baos.close();
baos=null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
CommentUDB helper = new CommentUDB(this);
SQLiteDatabase db = helper.getWritableDatabase();
Cursor cur = db.rawQuery("select * from ResponseTable1 where ResId = ?", new String[]{resid});
if(cur.getCount()>0){
}else {
ContentValues cValueImg = new ContentValues();
cValueImg.put("ResId", resid);
cValueImg.put("ResType",resType);
cValueImg.put("Response",data);
cValueImg.put("OriginalItemId",originalitemid);
db.insert("ResponseTable1", null, cValueImg);
System.out.println("the database is called");
db.close();
helper.close();
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
07-30 16:08:41.680: E/AndroidRuntime(11738): FATAL EXCEPTION: Thread-12083
07-30 16:08:41.680:
E/AndroidRuntime(11738): java.lang.OutOfMemoryError
07-30 16:08:41.680: E/AndroidRuntime(11738):
at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
07-30 16:08:41.680: E/AndroidRuntime(11738):
at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:518)
07-30 16:08:41.680: E/AndroidRuntime(11738):
at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:536)
07-30 16:08:41.680: E/AndroidRuntime(11738):
at com.inventit.commentu.getInformationService.insertResponseTable(getInformationService.java:258)
07-30 16:08:41.680: E/AndroidRuntime(11738):
at com.inventit.commentu.getInformationService$Worker.run(getInformationService.java:130)
07-30
16:08:41.740: I/Adreno200-EGLSUB(11738): <ConfigWindowMatch:2087>: Format RGBA_8888.
提前致谢。