0

I have some standard code to share an image in my Android app. The image exists on the storage and I provide an URI to the image. This all works fine.

However, this requires the WRITE_EXTERNAL_STORAGE permission. Is there a way I can share an image without the need of this permission, for example, to not save the image to storage, but specifying a memory stream or byte array?

Thanks!

Jasper
  • 545
  • 7
  • 18
  • How did you want to use this byte array? – dahui Apr 05 '16 at 09:18
  • Look at my answear [here](http://stackoverflow.com/questions/31826008/how-to-save-images-to-imageview-using-shared-preferences/31826212#31826212). It will works for you. – Eliasz Kubala Apr 05 '16 at 09:29

1 Answers1

0

You can convert an image file to a byte array with the following code, which I taken from an answer to a similar question: How to convert image into byte array and byte array to base64 String in android?

String filepath = "/sdcard/temp.png";
File imagefile = new File(filepath);
FileInputStream fis = null;
try {
    fis = new FileInputStream(imagefile);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
}

Bitmap bm = BitmapFactory.decodeStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
bm.compress(Bitmap.CompressFormat.JPEG, 100 , baos);    
byte[] b = baos.toByteArray(); 

Optional step to encode in Base64

encImage = Base64.encodeToString(b, Base64.DEFAULT);
Community
  • 1
  • 1
dahui
  • 2,128
  • 2
  • 21
  • 40