我正在设计一款具有媒体功能的应用,其中一款是视频捕捉。我正在使用相机的默认调用,而不是创建自己的相机。我遇到的问题是,在捕获视频后它没有保存到我指定的视频文件中,但是这个问题,到目前为止我已经测试过,只发生在使用相机应用程序的三星手机上。如果我在三星手机或其他公司的手机上使用Google相机,它的工作正常。这是三星手机的已知错误还是我做错了什么?如果它有解决方法吗?
这是我的videoHandler。
private void videoHandler() {
Intent intent = new Intent( MediaStore.ACTION_VIDEO_CAPTURE );
if ( intent.resolveActivity( getPackageManager() ) != null ) {
File videoFile = null;
try {
videoFile = createVideoFile();
} catch ( IOException e ) {
System.out.println( "Error creating file." );
}
if ( videoFile != null ) {
videoUri = Uri.fromFile( videoFile );
intent.putExtra( MediaStore.EXTRA_OUTPUT, videoUri );
intent.putExtra( MediaStore.EXTRA_VIDEO_QUALITY, 1 );
intent.putExtra( MediaStore.EXTRA_SIZE_LIMIT, 15000000L );
intent.putExtra( MediaStore.EXTRA_DURATION_LIMIT, 6 );
startActivityForResult( intent, VIDEO_REQUEST_CODE );
}
}
}
它调用createVideoFile,我在其中为目标创建视频文件。
private File createVideoFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat( "yyyyMMdd_HHmmss" ).format( new Date() );
String imageFileName = "MP4_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES );
File video = File.createTempFile(
imageFileName, /* prefix */
".mp4", /* suffix */
storageDir /* directory */
);
this.videoUri = Uri.fromFile( video );
Intent mediaScanIntent = new Intent( Intent.ACTION_MEDIA_SCANNER_SCAN_FILE );
mediaScanIntent.setData( this.videoUri );
this.sendBroadcast( mediaScanIntent );
return video;
}
这是我在onActivityResult中尝试调用视频时遇到的错误。
01-17 16:37:23.254 27162-28145/com.example.kevinem.blitz E/ImageLoader﹕ /storage/emulated/0/Pictures/MP4_20150117_163704_-918353230.mp4: open failed: ENOENT (No such file or directory)
java.io.FileNotFoundException: /storage/emulated/0/Pictures/MP4_20150117_163704_-918353230.mp4: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:409)
at java.io.FileInputStream.<init>(FileInputStream.java:78)
at java.io.FileInputStream.<init>(FileInputStream.java:105)
at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStreamFromFile(BaseImageDownloader.java:160)
at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStream(BaseImageDownloader.java:88)
at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.getImageStream(BaseImageDecoder.java:93)
at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.decode(BaseImageDecoder.java:73)
at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeImage(LoadAndDisplayImageTask.java:264)
at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:237)
at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:135)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
at libcore.io.IoBridge.open(IoBridge.java:393)
答案 0 :(得分:1)
我解决了我的问题。基本上我发现有人这样说。
不要求相机应用遵循ACTION_IMAGE_CAPTURE或ACTION_VIDEO_CAPTURE的规则。有些人会尊重特定的请求输出位置。有些人不会,并将返回他们选择将图像/视频存储在onActivityResult()上传递的结果Intent中包含的Uri的位置。有些可能完全被破坏,并且没有告诉您关于图像/视频存储位置的任何信息,即使存储了图像/视频。
然后我在另一篇文章中发现我不得不手动检索它。
if (resultCode == -1) {
//create a MediaScanner to save the vid into the indicate path
new MediaScannerConnectionClient() {
private MediaScannerConnection msc = null;
{
msc = new MediaScannerConnection(
getApplicationContext(), this);
msc.connect();
}
public void onMediaScannerConnected() {
msc.scanFile(name_vid, null);
}
public void onScanCompleted(String path, Uri uri) {
msc.disconnect();
}
};
Toast.makeText(this,"Video saved succesfull",
Toast.LENGTH_SHORT).show();
}
name_vid是您希望以String格式保存文件的路径。
答案 1 :(得分:0)
您使用File.createTempFile( )
是否有特殊原因?
使用,在默认临时文件目录中创建一个空文件 给定的前缀和后缀以生成其名称。调用此方法 相当于调用createTempFile(prefix,suffix,null)。
Files.createTempFile方法提供了另一种方法 在临时文件目录中创建一个空文件。创建的文件 该方法可能对文件具有更严格的访问权限 由此方法创建,因此可能更适合安全敏感 应用
不确定这是否产生预期结果。我建议你看看这个文档。
特别是这件作品。File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File file = new File(path, "DemoPicture.jpg");
return file.exists();
<强> TL;博士强>
我认为这将解决你的问题
File video = new File(storageDir, imageFileName + ".mp4")