Android Camera Video intent returns null URI

2019-05-27 04:51发布

问题:

I recently got updated to Android 4.3 and the stock video camera started acting a little weird whenever I started it with an Intent from my app.

At first it would just crash and say "Gallery stopped responding". After a little while, I was able to record a video, but clicking on done returned a null URI to my app, which made it crash!

So I set out testing a 2.3.4 device with the same code. The video app returned a proper URI I could use on that device. The same code worked perfectly fine before I got 4.3 (had 4.2.2 stock Galaxy Nexus)

Here's an activity that get a null URI from the stock camera app of 4.3 but works fine on devices with 4.2.2 and less.

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button pick = (Button) findViewById(R.id.button1);
    pick.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            startActivityForResult(takeVideoIntent, 123);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == RESULT_OK){
        if(requestCode == 123){
            VideoView videoView = (VideoView) findViewById(R.id.videoView1);
            videoView.setVideoURI(data.getData());
            Log.d("Video", "URI "+data.getData());
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}
}

What do I do so that this never happens? Does this mean that this will work differently with other camera apps on different manufacturer devices?

回答1:

I fixed this by changing the video intent to something like this:

Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
String fName = "VideoFileName.mp4";
File f = new File(fName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, CAMERA_VIDEO_REQUEST);

And in the Activity Result I got the video file path as follows:

File f = new File(Environment.getExternalStorageDirectory().toString());
for (File temp : f.listFiles()) {
     if (temp.getName().equals("VideoFileName.mp4")) {
         f = temp;
         break;
     }
}
//f is the video file...


回答2:

This could be a bug in the camera app, I solved it by retrieving the last video taken:

Uri getLastPhotoOrVideo(Context context) {
    String[] columns = { MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DATE_ADDED };

    ContentResolver cr = context.getContentResolver();
    Cursor cursor = cr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, columns, null, null,
            MediaStore.MediaColumns.DATE_ADDED + " DESC");

    int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    String path = cursor.getString(columnIndex);

    cursor.close();

    return Uri.fromFile(new File(path));
}


回答3:

On Android 4.3, permissions have been introduced. You need to start an Intent this way. See this post for changes : Android 4.3 behaviors

You need to start intent this way on Android OS 4.3 onwards:

Intent intent = new Intent(Intent.MediaStore.ACTION_VIDEO_CAPTURE);
...
if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent);
} else {
    Toast.makeText(context, R.string.app_not_available, Toast.LENGTH_LONG).show();
}

You also need to add android:restrictedAccountType tag in you manifest file:

<application ...
    android:restrictedAccountType="com.example.account.type" >

Read this too