How to get Base64 String from InputStream?

12,731

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.

Share:
12,731
raymondis
Author by

raymondis

Updated on June 14, 2022

Comments

  • raymondis
    raymondis almost 2 years

    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
    raymondis over 8 years
    but how to came from InputStream to Bitmap?
  • raymondis
    raymondis over 8 years
    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
    raymondis over 8 years
    awesome! thank you for your code! can you tell me what format.format(new Date... is?
  • ELITE
    ELITE over 8 years
    ohhh. that was used to format current date to dd_MM_yyyy_hh_mm_ss_aa this format.I'll edit the answer.
  • raymondis
    raymondis over 8 years
    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?
  • ELITE
    ELITE over 8 years
    byte[] decodedByte = Base64.decode(input.getBytes(), Base64.DEFAULT); and done