2010-01-22 41 views
4

因此,我们的应用程序可以选择拍摄照片或视频。如果用户拍摄了照片,我们可以使用MediaStore.Images.Media.insertImage函数将新图像(通过文件路径)添加到手机的画廊并生成content:// style URI。考虑到我们只有它的文件路径,对于捕获的视频是否有类似的过程?Android MediaStore insertVideo

+0

http://stackoverflow.com/questions/1925502/android-gallery-view-video-also-thumbnail-issues可能的重复。我在那里发布了回复。 – user287989 2010-03-07 15:10:22

回答

5

我也很感兴趣,你能找到解决方案吗?

编辑:解决方案是RTFM。基于“内容提供者”一章,这里是我的代码工作:

 // 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

回复:发布的解决方案。请注意,Java有BufferedInputStream和BufferedOutputStream,所以你不必自己去做。 – 2010-04-30 17:44:34

+0

这个怎么样:http://developer.android.com/reference/android/media/MediaScannerConnection.html – Sam 2014-11-13 14:01:52

+0

Jut a note:这现在不起作用。 – 2016-06-28 20:57:42

3

如果您的应用程序生成一个新的视频,你只是想给MediaStore一些元数据,你可以建立在此功能:

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:随着Android 4.4(奇巧),这种方法不再有效。

+0

http://stackoverflow.com/questions/2114168/android-mediastore-insertvideo#comment35248961_22373136 – Urboss 2014-04-14 17:44:36

+0

是的,整个媒体存储/提供商系统在4.4被彻底检修。任何瞄准4.4+的人都应该寻找更现代的解决方案。 FWIW,我不认为对这个多年来的问题的答案有一个倒退。 – acj 2014-04-14 18:15:16

+0

感谢您的回复,并为downvote感到遗憾,但由于我回答了这个问题,因此我花了大约0.5小时不会去任何地方,所以我认为保存其他Android开发人员也不会这么做。我希望你明白。我不认为针对较低的API会在这里产生影响,因为Android团队不会花时间针对刚刚发生的无证/不支持的行为创建兼容性措施。删除该答案是否会从downvote中恢复这些点?如果没有,我可以在移除之前删除downvote。 – Urboss 2014-04-15 14:23:27

9

下面是一个简单的“单一的基于文件的解决方案”:

每当你添加文件,让MediaStore内容提供者知道如何使用

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

主要优点是:工作有任何MIME类型MediaStore支持

无论你何时删除一个文件,让MediaStore内容提供商知道它使用

getContentResolver().delete(uri, null, null) 
+0

有关获取内容样式URI的问题。你怎么得到它? – Sam 2014-11-13 11:47:07

+0

似乎不适用于api 21 /棒棒糖。 – qix 2015-04-20 01:41:30

+0

@qix奇怪...它在我的身边直到API 22 ... – Pascal 2015-04-20 07:14:50

1

试试看看这个代码。它似乎为我工作。文件路径的

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); 

示例 -

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

这段代码返回null,最新的KitKat,Nexus 5.我想它只适用于已经在媒体存储中的视频,所以它更像是查询不插入... VID_20140313_114321和路径表明,因为它是标准路径和捕捉视频时为标准相机应用命名。 – Urboss 2014-04-14 17:27:42

+0

tnx很多!它的工作就像一个魅力 – pavle 2016-08-22 20:55:21

3

我无法得到Intent.ACTION_MEDIA_SCANNER_SCAN_FILE广播在API 21(棒棒堂)为我工作,但MediaScannerConnection没有工作,如:

 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); 
        } 
       }); 
相关问题