在android 7.0中找不到配置的root错误

时间:2017-06-07 10:32:10

标签: android

我收到此错误: failed to find configured root that contains /storage/emulated/0/android/data/ 有一些类似的问题,但他们使用其他位置来存储文件。 我在用:    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) 方法,这是我用来打开相机的代码:

 private void TakePicture() {
    photoFile = FileManager.generateFileStampedPhotoFile();
    PicturesHelper.TryTakePictureWithAnIntent(this, photoFile, REQUEST_IMAGE_CAPTURE);
}


public static File generateFileStampedPhotoFile()
{
    Uri photoFileUri=null;
    File photoFile=null;
    File outputDir=getPhotoDirectory();
    if(outputDir!=null)
    {
        String timeStamp=new SimpleDateFormat("yyyyMMDD_HHmmss").format(new Date());
        String photoFileName="IMG_"+timeStamp+".jpg";
         photoFile=new File(outputDir,photoFileName);
       // photoFileUri=Uri.fromFile(photoFile);

    }
    return  photoFile;
}


 public static void TryTakePictureWithAnIntent(Activity context,File 
   photoFile,int requestCode)
  {
    Intent takePictureIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if(takePictureIntent.resolveActivity(context.getPackageManager())!=null) //if user has camera?
    {

        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(context,
                    "com.myapp.fileprovider",
                    photoFile);
            takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);

            context.startActivityForResult(takePictureIntent, requestCode);
        }

    }
}


public static File getPhotoDirectory()
{
    File outputDir=null;
    String externalStorageState=Environment.getExternalStorageState();
    if(externalStorageState.equals(Environment.MEDIA_MOUNTED)) {
        File pictureDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        outputDir=new File(new File(pictureDir,"Adriagate"),"Online");
        if(!outputDir.exists())//V.P. if directory/ies not exist/s it will be created. mksdrs method follows file structure and creates directory by direcory if needed
        {
            if(!outputDir.mkdirs())
            {
                return null;
            }
        }
    }
    return outputDir;
}

我已将提供程序放入AndroidManifest:

  <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.myapp.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-files name="Whatever_Nothing_Works"/>

2 个答案:

答案 0 :(得分:0)

您将图像存储在Environment.getExternalStoragePublicDirectory()中。这不是FileProvider支持的根源之一。此外,您的XML有<external-files>,而且FileProvider不支持该元素(有效的元素都以-path结尾)。

选项包括:

  • 将图像存储位置更改为FileProvider支持的某个位置,并调整XML以匹配

  • 切换为my StreamProvider并使用<external-public-path dir="Pictures">

  • 将您的XML更改为<external-path name="whatever" path="Pictures">并希望获得最佳

答案 1 :(得分:-1)

在Menifest中添加

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

替换file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
       <external-files-path name="my_images" path="Android/data/com.example.package.name/files/Pictures" />
</paths>

您的文件存储在getExternalFilesDir()下。这映射到<external-files-path>,而不是<files-path>