Base64 Image Conversion In Android Resulting In Corrupted Image
I need to upload JSON to server so I tried to encode it using base64 and then sending it through JSON but while decoding the image at the server end, the image appears corrupted. I
Solution 1:
Okay its working for me. i have got the right image after i have decoded in the second activity. Just for checking i have an ImageView in Act1 and here i convert it into Base64String pass it as a string to Act2 and in Act2(in second activity i have a Android bot in the image view) i decode base 64 image and set the bitmap that i got decoding it.
Code of first activity:
publicclassActivityImageBase64extendsActivity {
ImageView ivOriginal;
String imageInBase64;
private Bitmap mBitmap;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_base64);
ivOriginal = (ImageView) findViewById(R.id.ivAct1);
ivOriginal.setDrawingCacheEnabled(true);
}
publicvoidstart(View v){
mBitmap = ivOriginal.getDrawingCache();
ByteArrayOutputStreambyteArray=newByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArray);
byte[] byteArr = byteArray.toByteArray();
imageInBase64 = Base64.encodeToString(byteArr, Base64.DEFAULT);
System.out.println(imageInBase64);
Intentintent=newIntent(ActivityImageBase64.this, ActivityImageBase64_2.class);
intent.putExtra("image", imageInBase64);
startActivity(intent);
}
}
Code in Second Activity(decode the string to bitmap)
publicclassActivityImageBase64_2extendsActivity {
ImageView ivBase64Image;
private String base64Image;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_image_base64_2);
ivBase64Image = (ImageView) findViewById(R.id.ivAndroidBot);
base64Image = getIntent().getStringExtra("image");
byte[] decodedString = Base64.decode(base64Image, Base64.DEFAULT);
Bitmapbase64Bitmap= BitmapFactory.decodeByteArray(decodedString, 0,
decodedString.length);
ivBase64Image.setImageBitmap(base64Bitmap);
}
}
Post a Comment for "Base64 Image Conversion In Android Resulting In Corrupted Image"