Getting nullpointer exception on capturing VIDEO f

2019-08-23 06:06发布

问题:

I tested my code on sony, samsung and HTC devices where it works fine. But it doesnt work on other devices like karbonn etc...

Here is my code

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

This is the onactivity result 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
        }
    }

This is the logcat of exception I am getting

    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)

Line no 359 is -----> if (cursor.moveToFirst()) {

回答1:

Try this..

change the below line

Cursor cursor = getContentResolver().query(fileUri, proj,
                    null, null, null);

as

Cursor cursor = getContentResolver().query(videoUri, proj,
                    null, null, null);

because your Uri path assigned as like this Uri videoUri = fileUri; then your added in cursor as fileUri

EDIT:

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:

The fileUri already has the Uri of the file. Try removing the cursor part and using the following instead:

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

Please let me know if this works.



回答3:

Try like below

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