0

I gained access to this folder using Document Tree Intent :-

content://com.android.externalstorage.documents/tree/primary%3AExampleApp%2FMedia%2F.hiddenMedia

The URI of a image present in the above folder :-

content://com.android.externalstorage.documents/tree/primary%3AExampleFolder%2FMedia%2F.hiddenMedia/document/primary%3AExampleFolder%2FMedia%2F.hiddenMedia%2FCristiano.jpg

Now I have that image as a DocumentFile and its URI from the above folder.

DocumentFile documentFile = DocumentFile.fromSingleUri(context, fileUri);

fileUri is the URI of the DocumentFile.

Note :- The files in the folder cannot be accessed via MediaStore API because the folder is hidden

Usually this DocumentFile can be either an Image or a Video file.
How do I copy the Image/Video from the DocumentFile to Pictures/My App using MediaStore API.

Thanks in Advance!

  • `from the DocumentFile to the public Gallery Folder` From the Document file? But DocumentFIle is not a location to begin with. ` public Gallery Folder` What do you consider to be the public gallery folder? If you have a DocumentFile uri then the file is already in such a storage location that it is scanned by the media store and hence visible with Gallery apps. – blackapps Mar 28 '22 at 10:44
  • But of course if you have a DocumentFile uri you can make a copy of the file using a mediastore uri that you obtain first. It is only pretty unclear where you want the copy to land. – blackapps Mar 28 '22 at 10:46
  • @blackapps The URI of the DocumentFile is of a Hidden folder that i gained access via Document Intent. and I want the copy to land on `Pictures/My Folder` – MapleDeveloper Mar 28 '22 at 11:02
  • `via Document Intent. ` Pretty vague! Why so vague? A few wordt more and it would have been exact. – blackapps Mar 28 '22 at 11:29
  • Well all you want is doable. But why dont you react to all the things i said? – blackapps Mar 28 '22 at 11:30
  • @blackapps I have changed and added more info to the question. Let me know if you need more Info. Since you mentioned its doable, Can you share how its possible? – MapleDeveloper Mar 28 '22 at 12:51
  • Just request a writable uri from the media store using the insert() method. Then open an inputstream for your source uri and an outputstream for the destination uri. Then copy. – blackapps Mar 28 '22 at 13:38

1 Answers1

0

Thanks to @blackapps!
All I had to do was to request a writable URI from the MediaStore using the insert() method and then open a InputStream for source URI and OutputStream for destination URI.

Code for above action :-

public void saveFile(Uri sourceUri, String fileName, String mimeType) throws IOException{

    ContentValues values = new ContentValues();
    Uri destinationUri;

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P){
        values.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
        values.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);

        if (fileName.endsWith(".mp4")){
            values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_MOVIES + "/MyFolder");
            destinationUri = context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
        } else {
            values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + "/MyFolder");
            destinationUri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        }

        InputStream inputStream = context.getContentResolver().openInputStream(sourceUri);
        OutputStream outputStream = context.getContentResolver().openOutputStream(destinationUri);

        IOUtils.copy(inputStream, outputStream);
        Toast.makeText(context, "The File has been saved!", Toast.LENGTH_SHORT).show();

}

Note :- You have to add the Commons-io dependency in your build.gradle file to access IOUtils.copy() function

If you find any better way to do this, Do share it!