Michael Horojanski
Michael Horojanski

Reputation: 4701

How can I retrieve a bitmap file from internal storage in Android?

I'm using this code to save a bitmap image into Android internal storage:

public boolean saveImageToInternalStorage(JSONObject jobj, int flag) {
    try {
        URL url = new URL("http://192.168.43.94" + jobj.get("location"));
        Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        FileOutputStream fos = null;

        // Use the compress method on the Bitmap object to write image to
        // the OutputStream

        if (flag == 0) {
            fos = context.openFileOutput("0_" + jobj.getString("id") + ".png", Context.MODE_PRIVATE);
        } else {
            fos = context.openFileOutput(jobj.getString("first_category") + "_" + jobj.getString("id") + ".png", Context.MODE_PRIVATE);
        }

        // Writing the bitmap to the out stream
        bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();
        System.out.println("Picture saved successfully");
        return true;
    } catch (Exception e) {
        Log.e("saveToInternalStorage()", e.getMessage());
        return false;
    }

How can I retrieve the bitmap file afterwards?

Upvotes: 0

Views: 229

Answers (1)

krystian71115
krystian71115

Reputation: 1937

You can load bitmap using this code:

InputStream is = context.openFileInput(filename);
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
// and now you can use bitmap

Upvotes: 2

Related Questions