从Camera捕获照片时获取IllegalArgumentException

时间:2018-01-26 14:26:03

标签: android android-camera-intent image-capture android-fileprovider

在我的项目中,我使用{{1>}给定 provider_paths.xml 文件:

FileProvider.getUriForFile

这是我在 标签内的AndroidManifest.xml中添加的:

<?xml version="1.0" encoding="utf-8"?>
<paths>
  <external-files-path name="StrengGeheim" path="/" />
</paths>

这是我的相机意图代码:

<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/provider_paths"/>
    </provider>

在文件Uri中,路径类似于 / storage / emulated / 0 / StrengGeheim / 06673da7-876f-4a53-9a3d-288ae033f7ac ,并且从库中存储图像的路径也是相同的。但是在拍摄照片和应用程序正在关闭时,我仍然会遇到错误。

private void cameraIntent() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    fileUri = getOutputMediaFileUri();
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    startActivityForResult(intent, CAMERA);
}

private Uri getOutputMediaFileUri() {
    try {
        return FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".provider",getOutputMediaFile());
    }
    catch (IOException ex){
        ex.printStackTrace();
        Toast.makeText(getContext(), MESSAGE_FAILED, Toast.LENGTH_SHORT).show();
    }
    return null;
}

这就是我处理OnActivityResult的方式:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.stegano.strenggeheim, PID: 19189
              java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/StrengGeheim/8bb91a47-d33f-428d-a6ee-1f974e61e63e.png
                  at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:712)
                  at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:401)
                  at com.stegano.strenggeheim.fragment.FragmentEncode.getOutputMediaFileUri(FragmentEncode.java:68)
                  at com.stegano.strenggeheim.fragment.FragmentEncode.cameraIntent(FragmentEncode.java:61)
                  at com.stegano.strenggeheim.fragment.FragmentEncode.access$100(FragmentEncode.java:35)
                  at com.stegano.strenggeheim.fragment.FragmentEncode$2.onClick(FragmentEncode.java:111)
                  at android.support.v7.app.AlertController$AlertParams$3.onItemClick(AlertController.java:1044)
                  at android.widget.AdapterView.performItemClick(AdapterView.java:318)
                  at android.widget.AbsListView.performItemClick(AbsListView.java:1165)
                  at android.widget.AbsListView$PerformClick.run(AbsListView.java:3134)
                  at android.widget.AbsListView$3.run(AbsListView.java:4049)
                  at android.os.Handler.handleCallback(Handler.java:789)
                  at android.os.Handler.dispatchMessage(Handler.java:98)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6541)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

<paths>
  <external-files-path name="StrengGeheim" path="/" />
</paths>

此配置适用于getExternalFilesDir()下存储的文件。根据您的错误,这不是您的文件存储位置。将其更改为:

<paths>
  <external-path name="StrengGeheim" path="."/>
</paths>