Android MediaStore insertVideo

后端 未结 5 669
予麋鹿
予麋鹿 2020-11-30 09:46

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

相关标签:
5条回答
  • 2020-11-30 09:50

    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.

    0 讨论(0)
  • 2020-11-30 09:53

    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));
    
    0 讨论(0)
  • 2020-11-30 09:54

    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);
                        }
                    } );
    
    0 讨论(0)
  • 2020-11-30 10:00

    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)
    
    0 讨论(0)
  • 2020-11-30 10:00

    Try this code. It seems working 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);
    

    Example of filePath -

    /storage/emulated/0/DCIM/Camera/VID_20140313_114321.mp4
    
    0 讨论(0)
提交回复
热议问题