Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MediaStore insertVideo

So our app has the option to take either a picture or a video. If the user takes a picture, we can use the MediaStore.Images.Media.insertImage function to add the new image (via a filepath) to the phone's gallery and generate a content:// style URI. Is there a similar process for a captured video, given that we only have it's filepath?

like image 587
Robert Avatar asked Jan 22 '10 00:01

Robert


4 Answers

Here is an easy 'single file based solution':

Whenever you add a file, let MediaStore Content Provider knows about it using

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(imageAdded)));

Main advantage: work with any mime type supported by MediaStore

Whenever you delete a file, let MediaStore Content Provider knows about it using

getContentResolver().delete(uri, null, null)
like image 72
Pascal Avatar answered Nov 13 '22 02:11

Pascal


I'm also interested, could you find a solution?

Edit: solution is RTFM. Based on the "Content Providers" chapter here is my code that worked:

        // Save the name and description of a video in a ContentValues map.  
        ContentValues values = new ContentValues(2);
        values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
        // values.put(MediaStore.Video.Media.DATA, f.getAbsolutePath()); 

        // Add a new record (identified by uri) without the video, but with the values just set.
        Uri uri = getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);

        // Now get a handle to the file for that record, and save the data into it.
        try {
            InputStream is = new FileInputStream(f);
            OutputStream os = getContentResolver().openOutputStream(uri);
            byte[] buffer = new byte[4096]; // tweaking this number may increase performance
            int len;
            while ((len = is.read(buffer)) != -1){
                os.write(buffer, 0, len);
            }
            os.flush();
            is.close();
            os.close();
        } catch (Exception e) {
            Log.e(TAG, "exception while writing video: ", e);
        } 

        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
like image 34
Gabor Avatar answered Nov 13 '22 01:11

Gabor


If your app is generating a new video and you simply want to give the MediaStore some metadata for it, you can build on 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);
}

EDIT: As of Android 4.4 (KitKat), this method no longer works.

like image 6
acj Avatar answered Nov 13 '22 02:11

acj


I was unable to get the Intent.ACTION_MEDIA_SCANNER_SCAN_FILE broadcast to work for me under API 21 (Lollipop), but the MediaScannerConnection does work, e.g.:

        MediaScannerConnection.scanFile(
                context, new String[] { path }, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        Log.d(TAG, "Finished scanning " + path + " New row: " + uri);
                    }
                } );
like image 4
qix Avatar answered Nov 13 '22 02:11

qix