How share images/videos in custom folders using ContentResolver/ or other way in Android ? Getting FileUriExposedException Exception

196 Views Asked by At

I have a custom folder in the Pictures directory, like this Pictures/MyFolder. It has images in MyFolder. Here is how to share the images using ContentResolver on MyFolder only. Or is any alternative ways to share the image. It is on android 11.

I queried images using the following snippets

  String[] projection = new String[]{
                MediaStore.Images.Media.DATA,
                MediaStore.Images.Media._ID,
                MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
                MediaStore.Images.Media.DATE_TAKEN};
        Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
        Cursor cur = context.getContentResolver().query(images,
                projection, // Which
                // columns
                // to return
                null, // Which rows to return (all rows)
                null, // Selection arguments (none)
                orderBy + " DESC" // Ordering
        );

 int bucketColumn = cur.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
            int dateColumn = cur.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN);
            do {
                bucket = cur.getString(bucketColumn);
                if (bucket.equals(context.getString(R.string.app_name))) {
                    date = cur.getString(dateColumn);
                 
                    imagePath = listImageByFolder.get(bucket);
                    if (imagePath == null) {
                        imagePath = new ArrayList<String>();
                    }
                    imagePath.add(cur.getString(cur.getColumnIndex(MediaStore.Images.Media.DATA)));

                    Uri x = Uri.withAppendedPath(MediaStore.Images.Media.INTERNAL_CONTENT_URI, String.valueOf(cur.getString(cur.getColumnIndex(MediaStore.Images.Media.DATA))));
                    listImageByFolder.put(bucket, imagePath);
                } ;
            } while (cur.moveToNext());


While I try to share the image, Getting android.os.FileUriExposedException: file:///storage/emulated/0/Pictures/WhatsTouch/1628090801008.jpg exposed beyond app through ClipData.Item.getUri() exception. Is any way to correct it or is any alternative way?

0

There are 0 best solutions below