在从相机,android获取VIDEO时获取nullpointer异常?

时间:2013-12-05 06:48:45

标签: android video camera nullpointerexception

我在sony,samsung和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);

这是onactivity结果代码

   @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
        }
    }

这是我得到的异常的logcat

    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()){

3 个答案:

答案 0 :(得分:2)

fileUri已经拥有该文件的Uri。尝试删除光标部分,然后使用以下代码:

File newFile = new File(videoUri.getPath());
String filePath = newFile.getAbsolutePath();

如果有效,请告诉我。

答案 1 :(得分:1)

试试这个..

更改以下行

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);

答案 2 :(得分:1)

尝试以下

Uri videoUri = data.getData();//Changed here
 String[] proj = { MediaStore.Video.Media.DATA };//Changed Here
 Cursor cursor = getContentResolver().query(videoUri, proj,
                    null, null, null);