Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

saved video doesn't appear in gallery

Tags:

android

I hope someone can help me with this problem.

I have an app which enables the user to take a video (.mp4) or pick existing one. If taking a video, the video is saved to a public directory and I triggered the Android to scan the file to add it to media gallery.

Problem is, taking the video is working fine, I can preview the finished video just fine in my app. But this same video won't appear in media gallery and won't be accessible by other apps -except the FileManager-, even though other .mp4 in the same folder appear in the list just fine.

Further info:

  • In FileManager app, The not-appearing files have icon video icon while the appearing ones got a thumbnail. I can trigger these not-appearing files to be added to media gallery apps by cut and paste the files in FileManager app (so I believe is not due to files being corrupted).
  • The scan code works fine for the my code that take images from existing camera app, it just won't work for the video ones...

Is there any need for additional permission for this to work? I've added/asked/request permission for write and read from ext. storage and camera in my manifest and code.

This below is how I take the Video and scan it into gallery :

private void takeVideo() {
    Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

    if (takeVideoIntent.resolveActivity(ctx.getPackageManager()) != null) {

        // Create the file where photo should go
        File mediaFile = null;

        try {
            mediaFile = getOutputMediaFile(ctx, MEDIA_TYPE_VIDEO);

        } catch (IOException ex) {
            Log.e("FragCamera", "takeVideo() : Error occurred while creating File." + ex);
        }

        if (mediaFile != null) {
            Uri mediaUri = Uri.fromFile(mediaFile);

            Log.d("FragCamera", "takeVideo() mediaUri: " + mediaUri);

            currMediaUri = mediaUri;
            currPhotoPath = mediaFile.getAbsolutePath();
            Log.d("FragCamera", "takeVideo() currPhotoPath: " + currPhotoPath);

            //make the new file available for other apps
            updateMediaGallery(mediaFile);

            MediaScannerConnection.scanFile(
                    ctx,
                    new String[]{currPhotoPath},
                    new String[]{"video/mp4"},
                    new MediaScannerConnection.OnScanCompletedListener() {
                        @Override
                        public void onScanCompleted(String path, Uri uri) {
                            Log.v("FragCameraScan",
                                    "file " + path + " was scanned seccessfully: " + uri);
                        }
                    });

            takeVideoIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mediaUri);
            this.startActivityForResult(takeVideoIntent, I_REQUEST_VIDEO_CAPTURE);
        }

    }
}

private void galleryAddPic(String filePath) {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(filePath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    ctx.sendBroadcast(mediaScanIntent);
}

Logcat value for each Log in the code above :

 D/FragCamera: takeVideo() mediaUri: file:///storage/emulated/0/DCIM/Camera/VID_20161207_142021.mp4
 D/FragCamera: takeVideo() currPhotoPath: /storage/emulated/0/DCIM/Camera/VID_20161207_142021.mp4
 V/FragCameraScan: file /storage/emulated/0/DCIM/Camera/VID_20161207_142021.mp4 was scanned seccessfully: null
like image 529
ayakashi-ya Avatar asked Dec 07 '16 07:12

ayakashi-ya


People also ask

Why are videos not showing in gallery?

Go to Settings -> Apps / Application manager -> search for Gallery -> open Gallery and tap on Clear Data . Switch off your phone and wait for few minutes (say 2-3 min) and then switch on and wait for few minutes.

Why my WhatsApp videos are not showing in gallery?

If you have disabled the media visibility settings on your WhatsApp, you won't be able to save the WhatsApp media files on your gallery. Instead, your WhatsApp will instantly download all the photos and videos on your gallery by changing the settings.


1 Answers

try using this function

public Uri addVideo(File videoFile) {
    ContentValues values = new ContentValues(3);
    values.put(MediaStore.Video.Media.TITLE, "My video title");
    values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
    values.put(MediaStore.Video.Media.DATA, videoFile.getAbsolutePath());
    return getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
}

the 'values' is simply meta data about the video

like image 108
Abdullah Raza Avatar answered Oct 23 '22 02:10

Abdullah Raza