I have an Android application which involves downloading and saving Media files (Images, Videos, Audio, Documents). Earlier I was creating a directory in File system and download and save these files in that directory. However, due to restrictions in storage access and the introduction of Scoped Storage, for android 11, we have resorted to using MediaStore APIs. The problem I am facing is while using ContentResolver.insert() API, I am getting null uri in some devices (most of which are Samsung, but also happening in OnePlus, Redmi, Vivo, POCO). Is there a way around this? I need this uri to access the DATA column to get complete file path.
My current code for saving Image is as follows:
ContentResolver resolver = getApplicationContext().getContentResolver();
ContentValues contentValues = new ContentValues();
Uri collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, "image.png");
contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + "/MyPicturesDirectory");
Uri result = resolver.insert(collection, contentValues);
For other media types, I have similar separate concrete implementations.