Search code examples
androidimagebitmapfileinputstreamfileoutputstream

Reading image from private storage android


I have stored some image in the device private storage like:

    InputStream inputStream = response.getEntity().getContent();
    FileOutputStream fileOutputStream = openFileOutput("someImage.jpeg", cont.MODE_PRIVATE);
    int read = 0;
    byte[] buffer = new byte[32768];
    while((read = inputStream.read(buffer)) > 0) {
        fileOutputStream.write(buffer, 0, read);
    }
    fileOutputStream.close();
    inputStream.close();

Then I want to read that image and store it as bitmap to show it to the user, I don't know what to do to accomplish this! I want to read the image again from the private storage, then convert it to bitmap. any experience?


Solution

  • What you need here is BitmapFactory.decodeStream(String name).

    First, get an inputstream:

    InputStream input = openFileInput("someImage.jpeg");
    

    Next, use that to get a bitmap:

    Bitmap bmp = BitmapFactory.decodeStream(input);
    

    Don't forget to close your inputstream afterwards!