问题
Model: Samsung SM-G900F (Android 5.0)
Code to launch camera:
File file = new File(getExternalFilesDir(null),"video.mp4");
Uri uri = FileProvider.getUriForFile(this, "example.com.myapplication.fileprovider", file);
final Intent takePictureIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION| Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
final List<ResolveInfo> resInfoList =
getPackageManager()
.queryIntentActivities(takePictureIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (final ResolveInfo resolveInfo : resInfoList) {
final String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, uri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
startActivityForResult(takePictureIntent, 42);
Log:
11-03 11:40:00.073 30377-30377/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sec.android.app.camera, PID: 30377
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2002, result=-1, data=Intent { act=inline-data dat=content://example.com.myapplication.fileprovider/external/Android/data/example.com.myapplication/files/video.mp4 (has extras) }} to activity {com.sec.android.app.camera/com.sec.android.app.camera.Camcorder}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.app.ActivityThread.deliverResults(ActivityThread.java:3974)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4017)
at android.app.ActivityThread.access$1400(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1471)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetLong(Native Method)
at android.database.CursorWindow.getLong(CursorWindow.java:524)
at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:75)
at android.database.CursorWrapper.getLong(CursorWrapper.java:106)
at com.sec.android.app.camera.Camera.onActivityResult(Camera.java:6956)
at android.app.Activity.dispatchActivityResult(Activity.java:6475)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3970)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4017)
at android.app.ActivityThread.access$1400(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1471)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Looks like video capturing works only with file uri and doesn't work with content uri.
Do you have the same issues? Could you suggest some general solution?
回答1:
This helped me:
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", f);
} else {
uri = Uri.fromFile(f);
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
回答2:
This is working for me.
private void launchCamera() {
Intent captureVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (captureVideoIntent.resolveActivity(getPackageManager()) != null)
{
Uri videoUri = null;
try {
videoUri = getOutputMediaFileUri();
} catch (Exception ex) {
// Error occurred while creating the File
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (videoUri != null) {
captureVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
startActivityForResult(captureVideoIntent, REQUEST_CAMERA);
}
}
}
/**
* Creating file uri to store image/video
*/
private Uri getOutputMediaFileUri() {
if(Build.VERSION.SDK_INT >= 24) {
// For android N you need to use the file provider
return FileProvider.getUriForFile(this, getResources().getString(R.string.file_provider_name), getOutputMediaFile());
} else {
return Uri.fromFile(getOutputMediaFile());
}
}
/**
* returning file to store image / video
*/
private File getOutputMediaFile() {
// External sdcard location
File mediaStorageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"DirectoryNameWhereYouWantToStore");
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(TAG, "Oops! Failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
return new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".mp4");
}
来源:https://stackoverflow.com/questions/40397084/video-capturing-crash-on-samsung-android-5-with-fileprovider