Dim
Dim

Reputation: 4807

Adding video to gallery

I have video located here:

/storage/emulated/0/Android/data/com.my.package/files/video_folder/video_full.mp4

And I wish that the gallery will scan it, so I added:

private fun addVideoToGallery(path: String) {
    val mediaScanIntent = Intent(Intent.ACTION_MEDIA_MOUNTED)
    val mCurrentPhotoPath = "file: $path"

    val file = File(mCurrentPhotoPath)
    val contentUri: Uri = Uri.fromFile(file)
    mediaScanIntent.data = contentUri
    sendBroadcast(mediaScanIntent)
}

Manifest:

   <activity android:name=".activities.SplashActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_MOUNTED" />
            <data android:scheme="file" />
        </intent-filter>
    </activity>

But I get error:

 java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from

Upvotes: 0

Views: 131

Answers (2)

blackapps
blackapps

Reputation: 9282

Those files are in your app app-specific folder.

The MediaStore will not scan such app specific folders and hence you will never see them in Gallery apps.

You have to copy those files first.

Or to a more public place and then let them scan.

Or copy them to the MediaStore directly. (request an uri using .insert(), open an output stream for the uri en copy your file to the stream).

Upvotes: 1

Krishna sheladiya
Krishna sheladiya

Reputation: 451

You can try this code to make your video reflect in gallery.

  1. In Kotlin:

    MediaScannerConnection.scanFile( activity, arrayOf(YOUR_FILE_ABSOLUTE_PATH), null ) { path, uri -> }

  2. In Java

    MediaScannerConnection.scanFile(activity, new String[]{fileDelete.getAbsolutePath()}, null, new MediaScannerConnection.OnScanCompletedListener() { public void onScanCompleted(String path, Uri uri) { } });

Upvotes: 0

Related Questions