因此,我们的应用程序可以选择拍摄照片或视频。如果用户拍摄了一张照片,我们可以使用MediaStore.Images.Media.insertImage函数将新图像(通过文件路径)添加到手机的图库中,并生成content://样式URI。捕获的视频是否也有类似的过程,因为我们只有它的文件路径?
发布于 2013-02-13 16:53:52
这是一个简单的“基于单文件的解决方案”:
每当您通过添加文件时,使用以下命令让MediaStore内容提供商知道
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(imageAdded)));主要优点:可以使用MediaStore支持的任何mime类型
无论何时你删除一个文件,让MediaStore内容提供商知道它使用
getContentResolver().delete(uri, null, null)发布于 2010-02-10 20:35:33
我也很感兴趣,你能找到一个解决方案吗?
编辑:解决方案是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));发布于 2012-08-01 02:52:29
如果你的应用程序正在生成一个新的视频,而你只是想给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);
}编辑:从Android4.4 (KitKat)开始,这个方法不再有效。
https://stackoverflow.com/questions/2114168
复制相似问题