Video not shown in android Gallery

Viewed 27

I generate a video in my app and I want it to be discoverable by the Photo/Gallery app. When the file is created I call addVideoToGallery, which uses FileProvider and then call ACTION_MEDIA_SCANNER_SCAN_FILE intent.

Please find all required code segments below. Any ideas as to why my file deos not appear in Photo/Gallery app?

private Uri getImageUri(File file) {
    return FileProvider.getUriForFile(this, this.getPackageName() + ".provider", file);
}

private void addVideoToGallery(File videoFile) {
    Uri contentUri = getImageUri(videoFile);
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

private File getVideoFile() {
    String filename = TEST_VIDEO_FILE_NAME + "_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".mp4";
    if (isExternalStorageWritable()) {
        currentFile = new File(getExternalFilesDir(null), filename);
    } else {
        currentFile = new File(getFilesDir(), filename);
    }

    return currentFile;
}

<provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.time.example.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"></meta-data>
        </provider>

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="external"
        path="Android/data/com.time.example/files" />
</paths>
0 Answers
Related