我正在尝试使用intent捕获视频然后将其存储在我的SD卡中,现在我想要的是获取该录制视频的路径,所以为此我编写了以下代码。
对于录制视频,我有以下代码。
record.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
File storageDir = new File(Environment
.getExternalStorageDirectory() + "/filetoupload/");
if (!storageDir.exists()) {
storageDir.mkdirs();
}
try {
imageToStore = new File(storageDir, "" + "video.mp4");
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(imageToStore));
startActivityForResult(intent, CAPTURE_VIDEO);
} catch (Exception e) {
e.printStackTrace();
}
}
});
为了得到它的路径,我写了下面的代码。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case CAPTURE_VIDEO:
Uri videoUri = data.getData();
String videopath = getRealPathFromURI(videoUri);
System.out.println("Path is" + videopath);
mEditPath = new EditText(this);
mEditPath.setText(videopath);
mEditPath.setVisibility(View.GONE);
new videopath().execute();
Intent intent = new Intent(RecordVideo.this,
SaveAudioVideo.class);
startActivity(intent);
break;
default:
break;
}
}
}
方式
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
我也试过将MediaStore.Images.Media.DATA
作为MediaStore.Videos.Media.DATA
传递,但这也无效。每当我在录制视频后按下“确定”按钮时,我总是会遇到NullPointerException
错误。
02-25 13:10:19.214: E/AndroidRuntime(9075): FATAL EXCEPTION: main
02-25 13:10:19.214: E/AndroidRuntime(9075): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1001, result=-1, data=Intent { dat=file:///mnt/sdcard/filetoupload/video.mp4 }} to activity {iqual.fidol_final/iqual.fidol_final.RecordVideo}: java.lang.NullPointerException
02-25 13:10:19.214: E/AndroidRuntime(9075): at android.app.ActivityThread.deliverResults(ActivityThread.java:3135)
02-25 13:10:19.214: E/AndroidRuntime(9075): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3178)
02-25 13:10:19.214: E/AndroidRuntime(9075): at android.app.ActivityThread.access$1100(ActivityThread.java:134)
02-25 13:10:19.214: E/AndroidRuntime(9075): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
02-25 13:10:19.214: E/AndroidRuntime(9075): at android.os.Handler.dispatchMessage(Handler.java:99)
02-25 13:10:19.214: E/AndroidRuntime(9075): at android.os.Looper.loop(Looper.java:154)
02-25 13:10:19.214: E/AndroidRuntime(9075): at android.app.ActivityThread.main(ActivityThread.java:4624)
02-25 13:10:19.214: E/AndroidRuntime(9075): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 13:10:19.214: E/AndroidRuntime(9075): at java.lang.reflect.Method.invoke(Method.java:511)
02-25 13:10:19.214: E/AndroidRuntime(9075): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
02-25 13:10:19.214: E/AndroidRuntime(9075): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
02-25 13:10:19.214: E/AndroidRuntime(9075): at dalvik.system.NativeStart.main(Native Method)
02-25 13:10:19.214: E/AndroidRuntime(9075): Caused by: java.lang.NullPointerException
02-25 13:10:19.214: E/AndroidRuntime(9075): at iqual.fidol_final.RecordVideo.getRealPathFromURI(RecordVideo.java:206)
02-25 13:10:19.214: E/AndroidRuntime(9075): at iqual.fidol_final.RecordVideo.onActivityResult(RecordVideo.java:183)
02-25 13:10:19.214: E/AndroidRuntime(9075): at android.app.Activity.dispatchActivityResult(Activity.java:4672)
02-25 13:10:19.214: E/AndroidRuntime(9075): at android.app.ActivityThread.deliverResults(ActivityThread.java:3131)
02-25 13:10:19.214: E/AndroidRuntime(9075): ... 11 more
第206行是
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
和第183行是
String videopath = getRealPathFromURI(videoUri);
我知道它清楚地表明NullPointerException
,但不知怎的,我无法弄明白为什么?