1

I am developing an android application in which there is a scenario that we need to convert a file's absolute path (/storage/emulated/0/..../1584362593949.pdf) to content URI(content://)

I have tried converting for the file types such as Audio and video with the help of content resolver and there was no issue I faced but stuck at converting for PDF documents.

Check the code snippet below

public Uri convertFileUriToContentUri(String filePath, String title) {
        String fileType = title.substring(title.lastIndexOf(".") + 1);
        File file = new File(filePath);
        Uri base = null;
        //creating content values of size 4
        ContentValues values = new ContentValues(4);
        long current = System.currentTimeMillis();

        switch (fileType) {
            case "mp3":
                values.put(MediaStore.Audio.Media.TITLE, "audio" + file.getName());
                values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
                values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/mp3");
                values.put(MediaStore.Audio.Media.DATA, file.getAbsolutePath());
                base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                break;
            case "pdf":
                // I am Stuck here as MediaStore does not provide support to documents
                break;
            case "mp4":
                values.put(MediaStore.Video.Media.TITLE, "video" + file.getName());
                values.put(MediaStore.Video.Media.DATE_ADDED, (int) (current / 1000));
                values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
                values.put(MediaStore.Video.Media.DATA, file.getAbsolutePath());
                base = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                break;
        }
        //creating content resolver and storing it in the external content uri
        ContentResolver contentResolver = context.getContentResolver();
        Uri newUri = contentResolver.insert(base, values);

        //sending broadcast message to scan the media file so that it can be available
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
        return newUri;

    }

Please help me find out the solution.

Rakesh L
  • 1,136
  • 4
  • 18
  • 44
  • Use `FileProvider` and `getUriForFile()` to get a `content` `Uri` for a file on the filesystem that your app can access. "there was no issue" -- I expect that you will have issues on Android 10+. – CommonsWare Mar 16 '20 at 13:28
  • This will help you : https://stackoverflow.com/q/7305504/1318946 – Pratik Butani Mar 16 '20 at 13:29
  • @PratikButani But it is for images, right ? – Rakesh L Mar 16 '20 at 13:31
  • The extension doesn't matter I think. – Pratik Butani Mar 16 '20 at 13:32
  • @CommonsWare Can you please mention on how to achieve this in elaborate manner ? – Rakesh L Mar 16 '20 at 13:32
  • @PratikButani . No.. If we notice that, we can notice that MediaStore is used. MediaStore does not have any static types for documents (like PDF) – Rakesh L Mar 16 '20 at 13:37
  • 1
    @RakeshL: `FileProvider` has been around for several years. See https://developer.android.com/reference/androidx/core/content/FileProvider and https://developer.android.com/training/secure-file-sharing/setup-sharing, in addition to countless blog posts, books, etc. – CommonsWare Mar 16 '20 at 13:47
  • @CommonsWare I will check this out . Thank you :-) – Rakesh L Mar 17 '20 at 05:16

0 Answers0