java.lang.IllegalArgumentException 关于使用相机拍摄图像
java.lang.IllegalArgumentException on Capture Image using camera
当我尝试使用相机捕捉图像时出现错误,
FATAL EXCEPTION: main
Process: com.test.driver, PID: 10512
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.test.driver/files/Pictures/JPEG_20180225_110337_390840290.jpg
android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:719)
android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:404)
这是我使用的代码,
Manifest.xml
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/com.test.driver/files/Pictures" />
</paths>
这里还有 Java 代码 来捕获图像,
private void chooseImageFromCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
}
if (photoFile != null) {
Globals.imageUri = android.support.v4.content.FileProvider.getUriForFile(activity, activity.getPackageName()+".provider", photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Globals.imageUri);
activity.startActivityForResult(takePictureIntent, cameraRequestCode);
}
}
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
Globals.imagePath = image.getAbsolutePath();
return image;
}
我也试过改变这个
Globals.imageUri = android.support.v4.content.FileProvider.getUriForFile(activity, activity.getPackageName()+".provider", photoFile);
到
Globals.imageUri = android.support.v4.content.FileProvider.getUriForFile(activity, "com.test.driver.provider", photoFile);
但它也不适合我。
我也在清单文件上尝试更改
android:authorities="${applicationId}.provider"
到
android:authorities="com.test.driver.provider"
它也不适合我。
您必须将 .
的路径设置为当前目录,如下所示:
<external-path name="my_images" path="." />
很高兴看到commonsguy github,如果我添加任何内容都是多余的。
当我尝试使用相机捕捉图像时出现错误,
FATAL EXCEPTION: main
Process: com.test.driver, PID: 10512
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.test.driver/files/Pictures/JPEG_20180225_110337_390840290.jpg
android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:719)
android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:404)
这是我使用的代码,
Manifest.xml
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/com.test.driver/files/Pictures" />
</paths>
这里还有 Java 代码 来捕获图像,
private void chooseImageFromCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
}
if (photoFile != null) {
Globals.imageUri = android.support.v4.content.FileProvider.getUriForFile(activity, activity.getPackageName()+".provider", photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Globals.imageUri);
activity.startActivityForResult(takePictureIntent, cameraRequestCode);
}
}
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
Globals.imagePath = image.getAbsolutePath();
return image;
}
我也试过改变这个
Globals.imageUri = android.support.v4.content.FileProvider.getUriForFile(activity, activity.getPackageName()+".provider", photoFile);
到
Globals.imageUri = android.support.v4.content.FileProvider.getUriForFile(activity, "com.test.driver.provider", photoFile);
但它也不适合我。
我也在清单文件上尝试更改
android:authorities="${applicationId}.provider"
到
android:authorities="com.test.driver.provider"
它也不适合我。
您必须将 .
的路径设置为当前目录,如下所示:
<external-path name="my_images" path="." />
很高兴看到commonsguy github,如果我添加任何内容都是多余的。