0

I am uploading High resolution and big image on Server. I am using API that accept image in Base64 converted String. I am converting Image into Base64 String and uploading to server but if Image is big then it gives OutOfMemory Exception. can any body suggest me how to solve this.

This is the function where i am converting myBitmap (Bitmap to upoload) in Base64 Encoded String to send on server. I don't want to compress image.

if(myBitmap!=null){
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
    byte[] b = baos.toByteArray();

        // I am passing encodedImg as image parameter.
    String encodedImg = new String(Base64.encodeBase64(b));
    try{
        baos.close();
        baos = null;
        b = null;
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}


// Below code is to open camera/gallery to select Image
btnCamera.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                String fileName = "profile.jpg";
                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.TITLE, fileName);
                values.put(MediaStore.Images.Media.DESCRIPTION, "Image capture by camera");

                imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
                intent.putExtra("crop", "true");
                intent.putExtra("aspectX", 3);
                intent.putExtra("aspectY", 4);
                intent.putExtra("outputX", 480);
                intent.putExtra("outputY", 640);
                intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
                intent.putExtra("noFaceDetection",true);
                startActivityForResult(intent, 1);
                a.dismiss();
            }
        });
        btnGalary.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.putExtra("crop", "true");
                intent.putExtra("aspectX", 3);
                intent.putExtra("aspectY", 4);

                intent.putExtra("outputX", 480);
                intent.putExtra("outputY", 640);

                intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
                intent.putExtra("noFaceDetection",true);
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Gallery"),2);
                a.dismiss();
            }
        });


//My onActivityResult

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

        switch (requestCode) {

        case 1:
            if (resultCode == RESULT_CANCELED)
                break;
            else if (resultCode == RESULT_OK) {
                try{
                    long imageId=ContentUris.parseId(imageUri);

                    myBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);          

                    isImageChanged = true;
                    profilePic.setScaleType(ScaleType.CENTER_CROP);
                    profilePic.setImageBitmap(myBitmap);
                    break;
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }

        case 2:
            if (resultCode == RESULT_CANCELED)
                break;
            try {
                Uri imageUri = data.getData();

                long imageId=ContentUris.parseId(imageUri);

                myBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);

                profilePic.setScaleType(ScaleType.CENTER_CROP);
                isImageChanged = true;
                profilePic.setImageBitmap(myBitmap);
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;
        }
    }
Sunny
  • 3
  • 3
  • Please explain your question with proper codes and references. It must be a code problem, as out of memory should not come, even if you are planning to upload 10MB image size. – abhy Jun 13 '13 at 11:08
  • Now can you help to solve this issue? – Sunny Jun 13 '13 at 12:05

1 Answers1

0

Probably you might be getting an exception on

byte[] b = baos.toByteArray();

To handle that you want to consider recycling your images.

Also, using Multipart is considered a better option, since you are using Base64 you might want to ponder on Jen's Post too.

One more suggestion is, if you are on API level 11 or more then you may consider using:

android:largeHeap="true"

Which will help you to carry out your conversions and processing safely(Provided that you are not leaking any variable). For more information on this, see the documentation on Application.

Let me know if you still face any issues.

Community
  • 1
  • 1
abhy
  • 933
  • 9
  • 13
  • Hi, Abhy I am using android:largeHeap=true and i can not recycle my image because if I can not register successfully than i need to use that Bitmap. – Sunny Jun 14 '13 at 12:20
  • Hi, I didn't get that "register successfully" part. Do you mean that the bitmap you are recycling you are trying to use it later somewhere in code? Please explain by pointing out where you are facing the problem exactly? – abhy Jun 17 '13 at 12:58
  • Yes i am using that bitmap. means i can not recycle that bitmap. – Sunny Jun 17 '13 at 13:48