Android MediaStore insertVideo

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

+4
source share
5 answers

Here is a simple single file solution:

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

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

Main advantage: work with any type of mime supported by MediaStore

Whenever you delete a file, let the MediaStore Content Provider know about it with

 getContentResolver().delete(uri, null, null) 
+9
source

I also wonder if you could find a solution?

Edit: The solution is RTFM. Based on the chapter "Content Providers", 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)); 
+5
source

If your application generates a new video, and you just want to provide some metadata for MediaStore for it, you can use 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 : with Android 4.4 (KitKat) this method no longer works.

+3
source

I was not able to broadcast the Intent.ACTION_MEDIA_SCANNER_SCAN_FILE broadcast for me according to API 21 (Lollipop), but MediaScannerConnection works, for example:

  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); } } ); 
+3
source

Try this code. It seems to work for me.

  filePath = myfile.getAbsolutePath(); ContentValues values = new ContentValues(); values.put(MediaStore.Video.Media.DATA, filePath); return context.getContentResolver().insert( MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values); 

FilePath example -

 /storage/emulated/0/DCIM/Camera/VID_20140313_114321.mp4 
+1
source

All Articles