Skip to content Skip to sidebar Skip to footer

Bitmap Too Large. Tried All

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 ple

Solution 1:

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

private String encodeToBase64(Bitmap image) {
  // get an InputStreamByteArrayOutputStreambaos=newByteArrayOutputStream(); 
  image.compress(CompressFormat.PNG, 0, baos); 
  byte[] bitmapdata = baos.toByteArray();
  ByteArrayInputStreambais=newByteArrayInputStream(bitmapdata);

  // prepare a buffer to read the inputStreambyte[] buffer = newbyte[10 * ONE_KIO];  // ONE_KIO = 1024int bytesRead;

  // prepare the stream to encode in Base64ByteArrayOutputStreamoutputStream=newByteArrayOutputStream();
  Base64OutputStreambase64OutputStream=newBase64OutputStream(outputStream, Base64.DEFAULT);

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

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

Solution 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
                            publicvoidonSuccess() {
                                BitmapDrawable drawable = (BitmapDrawable) picture.getDrawable();
                                yourSelectedImage = drawable.getBitmap();
                                //isImageSelected = true;

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

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

            }
        }

Post a Comment for "Bitmap Too Large. Tried All"