2

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!

raymondis
  • 356
  • 1
  • 5
  • 16

3 Answers3

4

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.

ELITE
  • 5,815
  • 3
  • 19
  • 29
  • awesome! thank you for your code! can you tell me what `format.format(new Date...` is? – raymondis Feb 09 '16 at 11:59
  • ohhh. that was used to format current date to `dd_MM_yyyy_hh_mm_ss_aa` this format.I'll edit the answer. – ELITE Feb 09 '16 at 12:00
  • I dont know why but my imageview is white after decoding from Base64 string to bitmap. Can you take a look in my decoding code? – raymondis Feb 09 '16 at 12:10
  • `byte[] decodedByte = Base64.decode(input.getBytes(), Base64.DEFAULT);` and done – ELITE Feb 09 '16 at 12:16
0

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.

Kezufru
  • 123
  • 10
0

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 .

Community
  • 1
  • 1
Shishupal Shakya
  • 1,632
  • 2
  • 18
  • 41
  • that was my first solution. But what if the user deletes the image? I want to save it in xml to make it independent from the original file – raymondis Feb 09 '16 at 11:29