1

I am building a chat application using socket.io. I want the images sent by the user to be viewed in the gallery.

I am currently using following code to save the image in application directory. how can I view images stored in "/data/data/com.example.diksha.chatapplication/" directory in gallery?

Code for saving the image

private Uri saveImage(Bitmap image){
        File directory = getContext().getFilesDir();
        String id = UUID.randomUUID().toString();
        File path = new File(directory, id + ".jpg");
        Log.d(TAG, "saveImage: " + path);
        try {
            FileOutputStream out = new FileOutputStream(path);
            image.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return FileProvider.getUriForFile(getContext(),getContext().getPackageName() + ".fileprovider", path);
    }
  • first You need to save to path of saved image in some persistent database . Then to view Use a answer from [This discussion](https://stackoverflow.com/questions/5383797/open-an-image-using-uri-in-androids-default-gallery-image-viewer). – ADM Mar 22 '18 at 08:51
  • `I am currently using following code to save the image in application directory. how can I view images stored in "/data/data/com.example.diksha.chatapplication/" directory in gallery?` You are saving them to your apps private directory. To `getFilesDir()`. The MediaStore and hence the Gallery app have no access. If instead you would save them to `getExternalStorageDirectory()` or `getExternalFilesDir()` you were done. Nothing else to do then. The MediaStore would find them after a while. – greenapps Mar 22 '18 at 13:06

1 Answers1

0

There are 2 approaches:

  1. First Approach:

Use below code to store your image into Gallery:

MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);

Later, you can see it when you open your Gallery.

  1. Second Approach:

Force Gallery to open your folder:

Intent i=new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(directory), "image/*");  
startActivity(i);
Cao Minh Vu
  • 1,900
  • 1
  • 16
  • 21