Android 如何使用文件提供程序获取图像Uri?

Android 如何使用文件提供程序获取图像Uri?,android,android-fileprovider,Android,Android Fileprovider,我和文件提供商有问题。我正在尝试获取我用手机相机拍摄的图像的Uri。但每当我尝试拍照时,它都会给我以下错误: FATAL EXCEPTION: main Process: com.example.android.inventory, PID: 30523 java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.example.android.inventory

我和文件提供商有问题。我正在尝试获取我用手机相机拍摄的图像的Uri。但每当我尝试拍照时,它都会给我以下错误:

FATAL EXCEPTION: main Process: com.example.android.inventory, PID: 30523 
java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.example.android.inventory/cache/IMG_20170718_213454_2102974580.jpg
                                                                               at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:679)
                                                                               at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:378)
                                                                               at com.example.android.inventory.EditorActivity$5.onClick(EditorActivity.java:239)                                                                 
onclick侦听器 雄激素单


文件提供程序路径


任何帮助都将不胜感激。谢谢。

您可以使用位图进行替换

//The method below is opened when button is clicked you can handle in your own way 
    private void OpenImageChooser() {
                Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
//on Activity result will give you the data when you can convert it into bitmap to show in the image view
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
                Bundle extras = data.getExtras();
                Bitmap imageBitmap = (Bitmap) extras.get("data");
                imgAttendance.setImageBitmap(imageBitmap);
            }
        }
    }
在AndroidMenifest.xml中


下面是从文件提供程序获取Uri的代码片段

File imageFile = createImageFile();


picUri = FileProvider.getUriForFile(RegisterActivity.this , this.getApplicationContext().getPackageName() + ".provider", imageFile);
下面是文件提供程序路径

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


由于位图质量的原因,这仅适用于缩略图。如果您需要完全大小的图像,您应该保存它。provider_paths.xml文件应该存储在哪里?我的意思是在哪个目录中?您可以将provider_paths.xml存储在“xml”目录中。文件路径应为res>xml>provider\u paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="share" path="/" />
</paths>
//The method below is opened when button is clicked you can handle in your own way 
    private void OpenImageChooser() {
                Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
//on Activity result will give you the data when you can convert it into bitmap to show in the image view
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
                Bundle extras = data.getExtras();
                Bitmap imageBitmap = (Bitmap) extras.get("data");
                imgAttendance.setImageBitmap(imageBitmap);
            }
        }
    }
<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>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
       <external-path name="external_files" path="."/>
</paths>
if (Build.VERSION.SDK_INT >= 24)
   mImageCaptureUri = FileProvider.getUriForFile(EditorActivity.this, BuildConfig.APPLICATION_ID + ".provider", f);
else
   mImageCaptureUri = Uri.fromFile(f);
File imageFile = createImageFile();


picUri = FileProvider.getUriForFile(RegisterActivity.this , this.getApplicationContext().getPackageName() + ".provider", imageFile);
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" 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>