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.