FileProvider无法找到包含...的配置根目录错误

时间:2017-12-14 16:20:24

标签: java android

我之前已经看过这个问题了,但在你将这个问题标记为多余之前,我只想说我已经试图弄清楚这个问题三个小时了,我只是感到困惑。我的目标是拍摄照片,将其设置为imageview,然后将其上传到Firebase存储。为了获得完整尺寸的照片,我相信我必须将其保存到本地文件,因为FileProvider会引用文件以及全尺寸照片的位置。到目前为止这是正确的吗?我无法为我的生活获得FileProvider来实现这一目标。

经过多次尝试,这就是我所拥有的:

// It all starts here
public void onClick(View v) {
    Log.d("DebugMySocks", "Change photo button clicked");
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(getApplicationContext(),
                    getPackageName(),
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
}

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp;
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        mProfileImage.setImageBitmap(imageBitmap);
    }
}

// in manifest file
<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="${applicationId}"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/filepaths" />
</provider>

// in /xml/filepaths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="com.example.kevin.moresocksplease/files/Pictures" />
</paths>

,错误信息为:

java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.kevin.moresocksplease/files/Pictures/JPEG_20171214_111337.jpg706457555.jpg

1 个答案:

答案 0 :(得分:0)

替换:

<external-path name="my_images" path="com.example.kevin.moresocksplease/files/Pictures" />

使用:

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