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;
}
}