Bitmap too large. Tried All

可紊 提交于 2021-01-29 05:34:55

问题


Guys I am facing this issue since 2 days straight. I want to pick an image from gallery and convert it to Base64 method. I have tried Picasso but my imageview is large. Can you please help me. I tried everything but out of memory is too much for me when converting it to bitmap and then to base64.

BitmapDrawable drawable = (BitmapDrawable) ProfilePic.getDrawable();
                            yourSelectedIBitmapDrawable drawable = (BitmapDrawable) ProfilePic.getDrawable();
                            yourSelectedImage = drawable.getBitmap();mage = drawable.getBitmap();

Code which is converting this bitmap to Base64. Is there any possibility of skipping the bitmap and directly converting to Base64

private String encodeToBase64(Bitmap image) {

        Bitmap immagex = image;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();
        String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);
        //Log.e("LOOK", imageEncoded);
        return imageEncoded;
    }

Profile Pic is name of Imageview.

EDIT: So guyz one of the ways is to use the large heap, but Google says you should not use that unless absolutely necessary. The other one that worked for me is the accepted answer. Now I came up with my own solution. In theory, I am using Picasso to load the image into image view. So what if I can extract the image from the ImageView. Not from URI or path, but from Imageview. This way you have an image which is already reduced by Picasso. Picasso provides Callback to for Success or failure. So on Success, you can access the cache memory of ImageView and extract the Bit map out of it.

I will post the code as an answer shortly. I tried it and even an image of 35Mb originally can be converted to bitmap without Out of memory exception.

So here is my ans: https://stackoverflow.com/a/52125006/6022584


回答1:


You can try to read the bitmap data with a buffer. Something like that :

private String encodeToBase64(Bitmap image) {
  // get an InputStream
  ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
  image.compress(CompressFormat.PNG, 0, baos); 
  byte[] bitmapdata = baos.toByteArray();
  ByteArrayInputStream bais = new ByteArrayInputStream(bitmapdata);

  // prepare a buffer to read the inputStream
  byte[] buffer = new byte[10 * ONE_KIO];  // ONE_KIO = 1024
  int bytesRead;

  // prepare the stream to encode in Base64
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  Base64OutputStream base64OutputStream = new Base64OutputStream(outputStream, Base64.DEFAULT);

  // read the inputStream
  while ((bytesRead = bais.read(buffer)) != -1) {
    base64OutputStream.write(buffer, 0, bytesRead);
  }
  base64OutputStream.close();

  // get the encoded result string
  return outputStream.toString();
}



回答2:


This is my Code that worked. New Issue is the name of Activity for me

if (requestCode == PICK_IMAGE) {
            if(resultCode == RESULT_OK){
                //isImageFromGallery = true;
                //isImageSelected = true;
                ImagePath = data.getData();

                Picasso.get()
                        .load(ImagePath)
                        .resize(1024,1024)
                        .onlyScaleDown()
                        .centerCrop()
                        .into(picture, new Callback(){
                            @Override
                            public void onSuccess() {
                                BitmapDrawable drawable = (BitmapDrawable) picture.getDrawable();
                                yourSelectedImage = drawable.getBitmap();
                                //isImageSelected = true;

                                Log.d("FileSize",Formatter.formatFileSize(NewIssue.this,
                                        yourSelectedImage.getByteCount()));
                            }

                            @Override
                            public void onError(Exception e) {
                                Toast.makeText(NewIssue.this, "Could Not Load Image", Toast.LENGTH_SHORT).show();
                            }
                        });

            }
        }


来源:https://stackoverflow.com/questions/52116446/bitmap-too-large-tried-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!