android android.provider.MediaStore.ACTION_VIDEO_CAPTURE 返回null onActivityResult 与nexus 7
Posted
技术标签:
【中文标题】android android.provider.MediaStore.ACTION_VIDEO_CAPTURE 返回null onActivityResult 与nexus 7【英文标题】:android android.provider.MediaStore.ACTION_VIDEO_CAPTURE return null onActivityResult with nexus 7 【发布时间】:2013-08-09 20:32:40 【问题描述】:我正在使用 Intent 录制视频。
所以我在recordVideo按钮的点击上使用以下代码
Videofilepath = "";
Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent,REQUEST_VIDEO_CAPTURED);
在 onActivityResult 中
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if (resultCode == Activity.RESULT_OK)
switch (requestCode)
case IMAGE_PICK:
this.imageFromGallery(resultCode, data);
break;
case IMAGE_CAPTURE:
this.imageFromCamera(resultCode, data);
break;
case REQUEST_VIDEO_CAPTURED:
this.videoFromCamera(resultCode, data);
break;
default:
break;
private void videoFromCamera(int resultCode, Intent data)
uriVideo = data.getData();
uploadedFileName="";
Constant.IS_FILE_ATTACH = true;
Toast.makeText(PDFActivity.this, uriVideo.getPath(), Toast.LENGTH_LONG)
.show();
String[] filePathColumn = MediaStore.Video.Media.DATA ;
Cursor cursor = getContentResolver().query(uriVideo, filePathColumn,
null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
Videofilepath = filePath;
System.out.println("Videofilepath filepath from camera : "
+ Videofilepath);
cursor.close();
File f = new File(filePath);
System.out.println("file created ? : " + f.exists());
Bitmap bMap = null;
do
try
// Simulate network access.
Thread.sleep(3000);
catch (InterruptedException e)
e.printStackTrace();
while (!f.exists());
bMap = ThumbnailUtils.createVideoThumbnail(filePath,
MediaStore.Video.Thumbnails.MICRO_KIND);
do
try
// Simulate network access.
Thread.sleep(1000);
catch (InterruptedException e)
e.printStackTrace();
while (bMap == null);
imageOrVideo = "video";
attachmentvalue.setImageBitmap(bMap);
此代码在三星 Galaxy Tab 上运行良好。但不适用于 Nexus 7。可能是 Nexus 7 有前置摄像头。 但我得到的结果数据意图是 null onActivityResult。
所以在我的 Logcat 中我得到了以下异常:-
08-08 12:51:31.160: E/AndroidRuntime(10899): FATAL EXCEPTION: main
08-08 12:51:31.160: E/AndroidRuntime(10899): java.lang.RuntimeException: Failure delivering result ResultInfowho=null, request=200, result=-1, data=Intent to activity com.example.activity/com.example.PDFActivity: java.lang.NullPointerException
【问题讨论】:
安卓4.3操作系统版本 【参考方案1】:创建一个分支并通过存储到一个永久目录来解决问题,该目录在收到结果后仍然可用。
https://github.com/pencilcheck/cordova-plugin-media-capture
【讨论】:
【参考方案2】:最后我解决了这个问题。
Nexus 7 将视频存储在DCIM
目录但onActivityResults
它返回null
。这是一个记录在案的 Nexus 7 设备问题。
所以用intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
解决这个问题
代码如下:-
记录按钮点击代码:-
intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFile(MEDIA_TYPE_VIDEO); // create a file to save the video in specific folder (this works for video only)
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high
// start the Video Capture Intent
startActivityForResult(intent, REQUEST_VIDEO_CAPTURED_NEXUS);
switch 内的代码 - onActivityResult 的 case 块:-
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if (resultCode == Activity.RESULT_OK)
switch (requestCode)
case REQUEST_VIDEO_CAPTURED_NEXUS:
this.videoFromCameraNexus(resultCode, data);
break;
default:
break;
// videoFromCameraNexus 方法
private void videoFromCameraNexus(int resultCode, Intent data)
if(fileUri != null)
Log.d(TAG, "Video saved to:\n" + fileUri);
Log.d(TAG, "Video path:\n" + fileUri.getPath());
Log.d(TAG, "Video name:\n" + getName(fileUri));
// use uri.getLastPathSegment() if store in folder
//use the file Uri.
使用以下方法获取输出媒体文件uri
public Uri getOutputMediaFile(int type)
// To be safe, you should check that the SDCard is mounted
if(Environment.getExternalStorageState() != null)
// this works for Android 2.2 and above
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), "SMW_VIDEO");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists())
if (! mediaStorageDir.mkdirs())
Log.d(TAG, "failed to create directory");
return null;
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if(type == MEDIA_TYPE_VIDEO)
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
else
return null;
return Uri.fromFile(mediaFile);
return null;
它对我有用。
【讨论】:
我对这个答案的问题是,如果您将 EXTRA_OUTPUT 位置传递给视频捕获意图,那么其他手机的大多数相机应用程序都无法正常运行。在一些较旧的三星设备上,相机应用程序在保存时会崩溃,在带有 android 4.1 的 Galaxy Note 1 上,相机应用程序将保存文件但不会给用户接受照片的按钮,它只是留在应用程序中(他们必须按不明显的后退按钮)。与其他应用程序/手机的混合结果也是如此。这意味着我们需要有逻辑来检测手机或相机应用...... 如果有人拒绝投票,请在此处发表评论,以便我纠正我的错误。 我在 Nexus 7 上遇到了同样的问题,但我还必须支持较旧的 Android 版本...是否有解决方法可以使其在 Nexus 7 和较旧的 Android 版本 (2.3) 上运行? `它是一个记录在案的 Nexus 7 设备问题`你能分享这个链接吗?【参考方案3】:感谢您的解决方法!
这里是更多刷和复制粘贴可用的代码:
/**
* Create intent to take video.
*/
public static Intent createTakeVideoIntent()
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
Uri uri = getOutputVideoUri(); // create a file to save the video in specific folder
if (uri != null)
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high
return intent;
@CheckForNull
private static Uri getOutputVideoUri()
if (Environment.getExternalStorageState() == null)
return null;
File mediaStorage = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "YOUR_APP_VIDEO");
if (!mediaStorage.exists() &&
!mediaStorage.mkdirs())
Log.e(YourApplication.TAG, "failed to create directory: " + mediaStorage);
return null;
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
File mediaFile = new File(mediaStorage, "VID_" + timeStamp + ".mp4");
return Uri.fromFile(mediaFile);
在 Nexus 4 v4.3 JWR66Y 上测试
【讨论】:
以上是关于android android.provider.MediaStore.ACTION_VIDEO_CAPTURE 返回null onActivityResult 与nexus 7的主要内容,如果未能解决你的问题,请参考以下文章
android.provider.Telephony.SMS_RECEIVED 的广播者权限