【问题标题】:How can I get image Uri using File Provider?如何使用文件提供程序获取图像 Uri?
【发布时间】:2017-07-19 04:41:18
【问题描述】:

我在使用文件提供程序时遇到问题。我正在尝试获取我用手机相机拍摄的图像的 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>

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

【问题讨论】:

标签: android android-fileprovider


【解决方案1】:

您也可以使用位图来完成。

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

【讨论】:

  • 这仅用于缩略图,因为位图质量。如果你需要全尺寸的图片,你应该保存它。
【解决方案2】:

在 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);

【讨论】:

    【解决方案3】:

    这里是从文件提供者获取 Uri 的代码 sn-p。

    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>
    

    这里&lt;external-path&gt;定义图像保存在外部存储中

    下面展示了如何在 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>
    

    【讨论】:

    • provider_paths.xml 文件应该存储在哪里?我的意思是在哪个目录?
    • 您可以将provider_paths.xml存储在“xml”目录中。文件路径应该是 res > xml > provider_paths.xml
    猜你喜欢
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 2021-11-05
    • 2017-08-25
    • 2021-02-24
    • 1970-01-01
    相关资源
    最近更新 更多