-1

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

提前致谢。

4

2 回答 2

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;}
于 2013-07-30T10:58:52.520 回答
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 开发者帮助

于 2013-07-30T11:08:18.620 回答