My code runs perfect on any Android lower then Android 11. On Android 11 (specifically on Pixel2 & Pixel3 emulator and real devices) the file descriptor fail to find the file after inserting it to the media store.
If I change the relative path on the second line to be only the Environment.DIRECTORY_MOVIES path - it works
String relativeLocation = Environment.DIRECTORY_MOVIES;
This is my code:
String mimeType = "video/mp4";
String relativeLocation = Environment.DIRECTORY_MOVIES + File.separator + SUB_DIRECTORY_NAME ;
shortFileName = getDateStamp() + selectedExtension;
videoContentValues = new ContentValues();
videoContentValues.put(MediaStore.Video.Media.TITLE, shortFileName);
videoContentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, shortFileName);
videoContentValues.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
videoContentValues.put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
videoContentValues.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis());
videoContentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativeLocation);
videoContentValues.put(MediaStore.MediaColumns.IS_PENDING, 1);
videResolver = mContext.getContentResolver();
Uri contentUri = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
videoUri = videResolver.insert(contentUri, videoContentValues);
if (videoUri == null) {
throw new IOException("Failed to create new MediaStore record.");
}
pfd = mContext.getContentResolver().openFileDescriptor(videoUri, "w");
mMediaRecorder.setOutputFile(pfd.getFileDescriptor());
The code runs OK until it gets to the openFileDescriptor which return the following error:
java.io.FileNotFoundException: java.io.FileNotFoundException: No item at content://media/external_primary/video/media/47
What am I doing wrong?
Update 1: More info: I am still requesting WRITE_EXTERNAL_STORAGE and making sure it is allowed before start recording
The following code works in the same camera app on Android 11 Pixel 2 emulator to save pics:
String mimeType = "image/jpeg";
final String relativeLocation = Environment.DIRECTORY_PICTURES + File.separator + SUB_DIRECTORY_NAME;
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, createImageFileNameQ());
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativeLocation);
ContentResolver resolver = mContext.getContentResolver();
OutputStream stream = null;
Uri uri = null;
try {
final Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
uri = resolver.insert(contentUri, contentValues);
try {
OutputStream fos = resolver.openOutputStream(uri);
fos.write(data);
fos.flush();
fos.close();
}
catch (Exception ex) {
Log.e(TAG, ex.toString());
}
Update 2:
If I write on the second line "String relativeLocation = Environment.DIRECTORY_PICTURES + File.separator + SUB_DIRECTORY_NAME;" and not DIRECTORY_MOVIES : It works!@!! Video files are saved on Videos\Sub_Directory
Update 3: Now on the emulators:
no trick helps to make relative path. And also the pics cannot be saved to relative path
String mimeType = "image/jpeg"; String relativeLocation = Environment.DIRECTORY_PICTURES + File.separator + SUB_DIRECTORY_NAME;
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, createImageFileNameQ());
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativeLocation);
ContentResolver resolver = mContext.getContentResolver();
OutputStream stream = null;
Uri uri = null;
try {
final Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
uri = resolver.insert(contentUri, contentValues);
try {
OutputStream fos = resolver.openOutputStream(uri);
fos.write(data);
fos.flush();
fos.close();
} catch (Exception ex) {
Log.e(TAG, ex.toString());
}
} catch (Exception e) {}
Uri contentUri = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
Try other collections. VOLUME_EXTERNAL? – NakeshaIt works!@!! Video files are saved on Videos\Sub_Directory
? You mean on Pictures/Sub_Directory i think. – Nakeshathey said: "interesting
me too. – NakeshaloadThumbnail
method doesn't provide an actual URI to the thumbnail. – Apparently