Reputation: 356
I'm on a problem by taking the selected gallery picture and want to save it first as Base64
String
in a XML file (for later use. For example if you exit the app and open it again).
As you can see I get the Image on a InputStream
But first of all the onClick
method:
public void onClick(DialogInterface dialog, int which) {
pictureActionIntent = new Intent(Intent.ACTION_GET_CONTENT);
pictureActionIntent.setType("image/*");
startActivityForResult(pictureActionIntent,GALLERY_PICTURE);
}
Now in the onActivityResult
method I want to store the image from InputStream
to Base64
String
.
case GALLERY_PICTURE:
if (resultCode == RESULT_OK && null != data) {
InputStream inputstream = null;
try {
inputstream = getApplicationContext().getContentResolver().openInputStream(data.getData());
Base64InputStream in = new Base64InputStream(inputstream,0);
} catch (IOException e) {
e.printStackTrace();
}
@EDIT This is what I do after creating the base64 String.
Bitmap bmp = base64EncodeDecode.decodeBase64(Items.get("image"));
Image1.setImageBitmap(bmp);
And this is the decoding Method:
public Bitmap decodeBase64(String input) {
byte[] decodedByte = Base64.decode(input, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
I tried to use Base64InputStream
but without success.
Can you give me a hint how to get from InputStream
to Base64
String
?
How many steps it will take doesn't matter.
I hope someone can help me!
Kind Regards!
Upvotes: 2
Views: 14235
Reputation: 5940
Write these lines in onActivityResult
method
try {
// get uri from Intent
Uri uri = data.getData();
// get bitmap from uri
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
// store bitmap to file
File filename = new File(Environment.getExternalStorageDirectory(), "imageName.jpg");
FileOutputStream out = new FileOutputStream(filename);
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, out);
out.flush();
out.close();
// get base64 string from file
String base64 = getStringImage(filename);
// use base64 for your next step.
} catch (IOException e) {
e.printStackTrace();
}
private String getStringImage(File file){
try {
FileInputStream fin = new FileInputStream(file);
byte[] imageBytes = new byte[(int)file.length()];
fin.read(imageBytes, 0, imageBytes.length);
fin.close();
return Base64.encodeToString(imageBytes, Base64.DEFAULT);
} catch (Exception ex) {
Log.e(tag, Log.getStackTraceString(ex));
toast("Image Size is Too High to upload.");
}
return null;
}
you can use base64
String of image.
Also don't forget to add permissions in AndroidManifest.xml
file READ_EXTERNAL_STORAGE
and WRITE_EXTERNAL_STORAGE
EDIT:: Decode base64 to bitmap
byte[] bytes = Base64.decode(base64.getBytes(), Base64.DEFAULT);
ImageView image = (ImageView) this.findViewById(R.id.ImageView);
image.setImageBitmap(
BitmapFactory.decodeByteArray(bytes, 0, bytes.length)
);
Hope it'll work.
Upvotes: 4
Reputation: 1672
If you are selecting image from Gallery then why you are saving it as Base64 string in xml file , you can reuse that image from gallery .
For this save image url in SharedPreferences and use that url again to show image .
Edit :
If you want to store it locally then you can use SQLite Database to store it , for more detail visit this link .
Upvotes: 0
Reputation: 123
This should work:
public static byte[] getBytes(Bitmap bitmap) {
try{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
stream.flush();
//bitmap.compress(CompressFormat.PNG, 98, stream);
bitmap.compress(CompressFormat.JPEG, 98, stream);
//bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
return stream.toByteArray();
} catch (Exception e){
return new byte[0];
}
}
public static String getString(Bitmap bitmap){
byte [] ba = getBytes(bitmap);
String ba1= android.util.Base64.encodeToString(ba, android.util.Base64.DEFAULT);
return ba1;
}
Got this code from something I use in an application, stripped it down to the most basic as far as i know.
Upvotes: 0