在android中转换base64 t0位图时获取outofmemory异常?

时间:2013-07-30 10:52:31

标签: android android-image

在我的应用程序中,我使用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.

提前感谢。

2 个答案:

答案 0 :(得分:0)

你可以使用这个..所以得到正确的方法......和解决方案。

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
   int width = bm.getWidth();
   int height = bm.getHeight();
   float scaleWidth = ((float) newWidth) / width;
   float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
   Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
   matrix.postScale(scaleWidth, scaleHeight);

// "RECREATE" THE NEW BITMAP
   Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
        matrix, false);
   return resizedBitmap;}

答案 1 :(得分:0)

你需要缩放图像的大小试试这个

public static Bitmap loadResizedBitmap( String filename, int width, int height, boolean exact ) {
    Bitmap bitmap = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile( filename, options );
    if ( options.outHeight > 0 && options.outWidth > 0 ) {
        options.inJustDecodeBounds = false;
        options.inSampleSize = 2;
        while (    options.outWidth  / options.inSampleSize > width
                && options.outHeight / options.inSampleSize > height ) {
            options.inSampleSize++;
        }
        options.inSampleSize--;

        bitmap = BitmapFactory.decodeFile( filename, options );
        if ( bitmap != null && exact ) {
            bitmap = Bitmap.createScaledBitmap( bitmap, width, height, false );
        }
    }
    return bitmap;
}

有关调整图片大小的详细信息,请访问 android developer help