Search code examples
androidimagefilebitmapandroid-gallery

How to add a Bitmap to the Gallery of Phone?


I'm developing image editing App and I need to save Edited Image in Gallery of Android so user can access it and use it.

I have tried to store Bitmap into External Storage by looking up on stackoverflow. But some Android Phones like OnePlus5 Do not have external Storage. So, I have tried storing image in Internal Storage of phone Using this code. Which looks works perfectly because it returns path of stored Image in Log. In code bitmap is Bitmap of Edited image.

        ContextWrapper wrapper = new ContextWrapper(getApplicationContext());
        File file = wrapper.getDir("Images", MODE_PRIVATE);
        file = new File(file, "DemoImg2"+ ".jpg");
        try {
            OutputStream stream = null;
            stream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            stream.flush();
            stream.close();

        }
        catch (IOException e){
            e.printStackTrace();
        }

        Uri savedImagePath = Uri.parse(file.getAbsolutePath());

        Toast.makeText(getApplicationContext(), savedImagePath.toString(), Toast.LENGTH_LONG).show();
        Log.i("Path:", savedImagePath.toString());

But I can't find /data/user/0/com.packagename.blurd1/app_Images/DemoImg2.jpg anywhere from file manager. Saved Image also does not appear anywhere in Gallery . After searching about it, I know that only app, that stored image itself, can access image stored in Internal Memory and user can't access it. So what should I do to save Bitmap Image into gallery Directly even if android phone does not have external storage. I can't find any answer about it anywhere.


Solution

  • Every device has external storage. May be you are understanding wrong as external storage on Android phones doesn't mean memory on memory card. It means memory like 8GB,16GB,32GB etc that every phone company provides. So you need to save that image on external storage and later you can add that image to your gallery.

    See the link below, some portion of of this tutorial helps you to store image and add them to gallery.

    https://developer.android.com/training/camera/photobasics.html