【问题标题】:Android convert image from gallery into base64 result in OutOfMemory ExceptionAndroid 将图片从图库转换为 base64 导致 OutOfMemory 异常
【发布时间】:2013-06-24 14:44:07
【问题描述】:

我想从图库中加载图片,然后将其转换为 base64。

这听起来并不难。所以我这样做了:

首先打开图库并选择图片:

picteureBtn.setOnClickListener(new View.OnClickListener() {
            private Uri imageUri;

            public void onClick(View view) {
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);

            }
        });

第二个onActivityResult:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            picturePath = cursor.getString(columnIndex);
            cursor.close();


        }


    }

最后,我想解码我的image,它位于picutrePath

                String b64;
                StringEntity se;
                String entityContents="";
                if (!picturePath.equals("")){
                    Bitmap bm = BitmapFactory.decodeFile(picturePath);
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
                    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);

                    byte[] b = baos.toByteArray(); 
                    b64=Base64.encodeToString(b, Base64.DEFAULT);
                }

不幸的是我得到了:

06-24 16:38:14.296: E/AndroidRuntime(3538): FATAL EXCEPTION: main
06-24 16:38:14.296: E/AndroidRuntime(3538): java.lang.OutOfMemoryError
06-24 16:38:14.296: E/AndroidRuntime(3538):     at java.io.ByteArrayOutputStream.toByteArray(ByteArrayOutputStream.java:122)

谁能指出我做错了什么?

【问题讨论】:

    标签: java android memory


    【解决方案1】:

    我建议改变

    Bitmap bm = BitmapFactory.decodeFile(picturePath);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    //added lines
    bm.recycle();
    bm = null;   
    //added lines 
    byte[] b = baos.toByteArray(); 
    b64=Base64.encodeToString(b, Base64.DEFAULT);
    

    这样,您不会将位图两次加载到应用程序的内存中。

    【讨论】:

      【解决方案2】:

      有很多文章都在谈论这个,基本上你需要在尝试将其解码为Bitmap 之前计算尺寸,看看BitmapFactory 类。我有一个替代的解决方案给你,因为你是从图库中挑选一张图片,你可以在activityForResult 中获取Bitmap,如下所示:

      Bitmap image = (Bitmap) data.getExtras().get("data");
      

      您可以启动您的Intent 来获取如下图像:

      Intent intent = new Intent();    
      intent.setType("image/*");
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      startActivityForResult(intent, RESULT_LOAD_IMAGE);
      

      【讨论】:

        猜你喜欢
        • 2015-04-22
        • 2011-09-03
        • 1970-01-01
        • 1970-01-01
        • 2021-03-26
        • 2020-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多