使用相机捕获图像上的java.lang.IllegalArgumentException

时间:2018-02-25 08:21:52

标签: android android-camera android-fileprovider

当我尝试使用相机拍摄图像时,我收到错误,

ScrollView

这是我正在使用的代码,

的Manifest.xml

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)

file_paths.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>

此处还有用于捕获图像的 Java代码

<?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>

我也试过改变这个

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"

它也不适合我。

1 个答案:

答案 0 :(得分:1)

您必须将.的路径设置为当前目录,如下所示:

<external-path name="my_images" path="." /> 

很高兴看到commonsguy github,如果我添加任何内容是多余的。