android - getUriForFile() 抛出无法找到包含的已配置根

标签 android image camera filepath android-fileprovider

我正在制作一个应用程序,我需要拍照,然后在数据库中保存一些数据,但是当我尝试从照片中获取 URI 时,我收到此错误:

java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/br.com.xxxxx.fotoaula/files/Pictures/JPEG_20160915_161210_-695228406.jpg

这是我的代码

package br.com.xxxxx.fotoaula;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class PicturesActivity extends AppCompatActivity {

    private static final String TAG = "PicturesActivity";
    private static final int REQUEST_IMAGE_CAPTURE = 1;
    private FloatingActionButton fabNewPicture;
    private String mCurrentPhotoPath;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pictures);

        imageView = (ImageView)findViewById(R.id.imageView);
        fabNewPicture = (FloatingActionButton)findViewById(R.id.fab_new_picture);
        fabNewPicture.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                dispatchTakePictureIntent();
            }
        });
    }

    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 = "file:" + image.getAbsolutePath();
        return image;
    }

    private void dispatchTakePictureIntent()
    {
        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
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                Toast.makeText(getApplicationContext(), "Erro ao salvar imagem!", Toast.LENGTH_SHORT).show();
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "br.com.xxxxx.fotoaula.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Toast.makeText(this, "Imagem capturada com sucesso, salvar dados no BD", Toast.LENGTH_SHORT).show();
        }
    }
}

我的提供商:

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="br.com.xxxxx.fotoaula.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"></meta-data>
        </provider>

还有我的 file_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="Android/data/br.com.xxxxx.fotodata/files/Pictures" />
</paths>

我完全按照文档做的,你们能帮我找到解决方案吗?

非常感谢!

最佳答案

第 1 步:切换到最新版本的支持库(截至 2016 年 9 月 15 日的24.2.1)

第 2 步:将 file_paths.xml 更改为:

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

看看你是否有更好的运气。

关于android - getUriForFile() 抛出无法找到包含的已配置根,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39518856/

相关文章:

android - 创建新应用程序时,API 17 : Android 4. 2 与 Google API (Google inc.) (API 17)

PHP 裁剪图像的形状不是正方形,如椭圆形或圆形

c# - 从 Wiimote 转向相机?

android - Cwac 相机添加缩略图

android - Kotlin/Room 错误 : Parameter specified as non-null is null: method kotlin. jvm.internal.Intrinsics.checkParameterIsNotNull,参数消息

android - 无法解析 : `com.google.android.gms:play-services-appindexing:10.0.0`

css - HTML5 + CSS 在文本/填充之上重叠图像

gps - 使用 Camera Access 和 GPS 开发应用程序

java - Android RadioButton 逻辑

javascript - 如何在 JavaScript 中显示堆叠在一起的多张图片