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

时间:2017-07-19 04:41:18

标签: 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/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)                                                                 

onClickListener

//press button to take a photo
    final Button addImage = (Button) findViewById(R.id.click);
    addImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            try {
                File f = createImageFile();

                Log.d(LOG_TAG, "File: " + f.getAbsolutePath());

                mImageUri = FileProvider.getUriForFile(
                        EditorActivity.this, FILE_PROVIDER_AUTHORITY, f);

                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);

                if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    });

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name="com.example.android.inventory.CatalogActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.android.inventory.EditorActivity"
        android:theme="@style/EditorTheme"
        android:parentActivityName="com.example.android.inventory.CatalogActivity" >
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.android.inventory.CatalogActivity" />
    </activity>
    <provider
        android:name="com.example.android.inventory.data.InventoryProvider"
        android:authorities="com.example.android.inventory"
        android:exported="false" />

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.example.android.myfileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_provider_paths" />
    </provider>
</application>

<uses-feature android:name="android.hardware.camera"
    android:required="true" />

文件提供程序路径

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="share" path="/" />
</paths>

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

3 个答案:

答案 0 :(得分:0)

您可以使用Bitmap替代地执行此操作。

//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);
            }
        }
    }

答案 1 :(得分:0)

  

在AndroidMenifest.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

<paths xmlns:android="http://schemas.android.com/apk/res/android">
       <external-path name="external_files" path="."/>
</paths>
  

文件创建后的OnclickListener

if (Build.VERSION.SDK_INT >= 24)
   mImageCaptureUri = FileProvider.getUriForFile(EditorActivity.this, BuildConfig.APPLICATION_ID + ".provider", f);
else
   mImageCaptureUri = Uri.fromFile(f);

答案 2 :(得分:0)

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

File imageFile = createImageFile();


picUri = FileProvider.getUriForFile(RegisterActivity.this , this.getApplicationContext().getPackageName() + ".provider", imageFile);

以下是文件提供商路径

  

provider_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="."/>
</paths>

此处<external-path>定义该图片保存在外部存储空间

下面显示了如何在Manifest文件中定义文件。

<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>
相关问题