我在服务器上上传高分辨率和大图像。我正在使用接受 Base64 转换字符串中的图像的 API。我正在将 Image 转换为 Base64 String 并上传到服务器,但如果 Image 很大,那么它会给出 OutOfMemory 异常。任何人都可以建议我如何解决这个问题。
这是我将 Base64 编码字符串中的 myBitmap(Bitmap 到 upoload)转换为在服务器上发送的函数。我不想压缩图像。
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;
}
}