我在索尼、三星和HTC设备上测试了我的代码,这些设备运行良好。但它在其他设备上不起作用,比如karbonn等…
这是我的代码
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); // create
intent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri); // set the video file name
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent,CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
这是活动结果代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Video captured and saved to fileUri specified in the Intent
Uri videoUri = fileUri;
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(videoUri, proj,
null, null, null);
if (cursor.moveToFirst()) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
filePath = cursor.getString(column_index);
}
cursor.close();
Log.d("LOGCAT", "Video path is: " + filePath);
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(filePath,
MediaStore.Images.Thumbnails.MINI_KIND);
Toast.makeText(this, "Video saved to:n" + filePath,
Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the video capture
} else {
// Video capture failed, advise user
}
}
这是我得到的异常日志
FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=200, result=-1, data=Intent { dat=file:///mnt/sdcard/ext_sdcard/Pictures/Crime/VID_20131205_121435.mp4 }} to activity {com.pstpl.crime/com.pstpl.crimeverify.CrimeReportActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
at android.app.ActivityThread.access$1100(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.pstpl.crimeverify.CrimeReportActivity.onActivityResult(CrimeReportActivity.java:359)
at android.app.Activity.dispatchActivityResult(Activity.java:4662)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
第359行是----->if(cursor.moveToFirst()){
fileUri
已经具有该文件的Uri。尝试删除光标部分并使用以下内容:
File newFile = new File(videoUri.getPath());
String filePath = newFile.getAbsolutePath();
请告诉我这是否有效。
试试这个。。
更改线下
Cursor cursor = getContentResolver().query(fileUri, proj,
null, null, null);
作为
Cursor cursor = getContentResolver().query(videoUri, proj,
null, null, null);
因为你的Uri路径被分配为这样的Uri videoUri = fileUri;
,然后你添加的游标被分配为fileUri
编辑:
try {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(videoUri, proj,
null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(proj[0]);
filePath = cursor.getString(columnIndex);
} catch (Exception e) {
filePath = videoUri.getPath();
}
Log.v("log", "filePath is : " + filePath);
试试下面的
Uri videoUri = data.getData();//Changed here
String[] proj = { MediaStore.Video.Media.DATA };//Changed Here
Cursor cursor = getContentResolver().query(videoUri, proj,
null, null, null);