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 corru
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:
public class ActivityImageBase64 extends Activity {
ImageView ivOriginal;
String imageInBase64;
private Bitmap mBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_base64);
ivOriginal = (ImageView) findViewById(R.id.ivAct1);
ivOriginal.setDrawingCacheEnabled(true);
}
public void start(View v){
mBitmap = ivOriginal.getDrawingCache();
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArray);
byte[] byteArr = byteArray.toByteArray();
imageInBase64 = Base64.encodeToString(byteArr, Base64.DEFAULT);
System.out.println(imageInBase64);
Intent intent = new Intent(ActivityImageBase64.this, ActivityImageBase64_2.class);
intent.putExtra("image", imageInBase64);
startActivity(intent);
}
}
Code in Second Activity(decode the string to bitmap)
public class ActivityImageBase64_2 extends Activity {
ImageView ivBase64Image;
private String base64Image;
@Override
protected void onCreate(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);
Bitmap base64Bitmap = BitmapFactory.decodeByteArray(decodedString, 0,
decodedString.length);
ivBase64Image.setImageBitmap(base64Bitmap);
}
}