当前位置: 首页 > 知识库问答 >
问题:

JAVAlang.IllegalArgumentException:未能找到包含/storage/emulated/0/Pictures的已配置根目录/

宗翔宇
2023-03-14

寻找一些错误的帮助,我正在尝试存储我正在尝试开发的应用程序的相机拍摄的照片。错误是

JAVAlang.IllegalArgumentException:未能找到包含/storage/emulated/0/Pictures/JPEG20161108_153704的已配置根目录_

logcat在我的代码中FileProvider所在的行中指向这个方法。正在调用getUriForFile。。

private void dispatchTakePhoto() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivity(takePictureIntent); // this worked originally
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException e) {
                e.printStackTrace();
                Log.e(TAG, ""+e);
            }
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(TallyActivity2.this,
                        "com.example.bigdaddy.pipelinepipetally.fileprovider", photoFile);
                takePictureIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
    }

此方法用于创建图像文件

 private File createImageFile() throws IOException {
        /* Create an image file name */
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
        String imageFileName = "JPEG" + timeStamp + "_";
        File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile(), imageFileName);
        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

        /* Tried this also, not working. Leaving for debugging.
        File image = File.createTempFile(
                imageFileName,
                ".jpg",
                storageDir
        );*/

        File image = new File(path, imageFileName);
        try {
            /* Making sure the Pictures directory exist.*/
            path.mkdir();
            storageDir.createNewFile();
        }catch (Exception e) {
            e.printStackTrace();
        }

        /* Save a file: path for use with ACTION_VIEW intents */
        mCurrentPhotoPath = "file:" + image.getAbsolutePath();
        return image;
    }

这里是onActivityResult()方法,其中我想要将捕获的图像保存到一个类中,并将缩略图设置为ImageView。SaveImage()方法返回一个字节,这样我就可以通过Intent将Bundle中的字节传递到另一个全屏活动,如果用户单击缩略图ImageView,它将在那里显示。

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /* If it's equal to my REQUEST_IMAGE_CAPTURE var, we are all good. */
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            /*Saving to the Pipe class with the saveImage() method below.*/
            sDummyImagePicByte = saveImage(imageBitmap);
            /* Going ahead an setting the thumbnail here for the picture taken*/
            mPipePicImage.setImageBitmap(imageBitmap);
            Log.i(TAG,Arrays.toString(sDummyImagePicByte)+" after assignment from saveImage()");
        }
    }

这是清单。xml文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          package="com.example.bigdaddy.pipelinepipetally">

    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="18"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>

    <application
        android:allowBackup="true"
        android:debuggable="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="HardcodedDebugMode">
        <activity
            android:name=".MainActivity"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".TallyActivity2"
            android:windowSoftInputMode="adjustResize">
        </activity>
        <activity
            android:name=".JobAndDbActivity"
            android:windowSoftInputMode="adjustResize">
        </activity>
        <activity
            android:name=".ExistingTallyActivity"
            android:windowSoftInputMode="adjustResize">
        </activity>
        <activity android:name=".ImageToFullscreen"
            android:windowSoftInputMode="adjustResize">
        </activity>

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

    </application>
</manifest>

以下是文件路径。xml我创建的文件,并放入app/res/xml/文件夹(我也创建了该文件夹)。不确定这是否是文件夹的正确位置。

<paths >
    <files-path name="my_images" path="files/"/>
    ...
</paths>

这也是onRequestPermissionsResult()的

 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_CAMERA:
                /* if request is canceled, the result arrays are empty */
                if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    /* Permissions granted so the mPermissionIsGranted boolean is set to true*/
                    mPermissionIsGranted = true;
                } else {
                    /*
                    Permissions denied so the mPermissionIsGranted boolean stays false here and
                    providing a Toast message to the user, letting them know that camera permissions
                    are required for this feature.
                    */
                    Toast.makeText(getApplicationContext(),"Camera permissions required\nfor this" +
                                    "feature.",
                            Toast.LENGTH_LONG).show();
                    /* Continuing to hold the false setting to this boolean since not granted.*/
                    mPermissionIsGranted = false;
                }
                break;
            /* For accessing and writing to the SD card*/
            case MY_PERMISSIONS_REQUEST_SD_CARD:
                /* if request is canceled, the result arrays are empty */
                if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    /* Permissions granted so the mPermissionIsGranted boolean is set to true*/
                    mPermissionIsGranted = true;
                } else  {
                    /*
                    Permissions denied so the mPermissionIsGranted boolean stays false here and
                    providing a Toast message to the user, letting them know that camera permissions
                    are required for this feature.
                    */
                    Toast.makeText(getApplicationContext(),"SD Card permissions required\nfor this"+
                                    "feature.",
                            Toast.LENGTH_LONG).show();
                    /* Continuing to hold the false setting to this boolean since not granted.*/
                    mPermissionIsGranted = false;
                }
                break;
            /* For the GPS location permissions.*/
            default: MY_PERMISSIONS_REQUEST_FINE_LOCATION:
            /* Still to be implemented .*/
                break;
        }
    }

我非常感谢你在这方面的帮助。我还是个新手,正在学习Android。提前谢谢。


共有3个答案

益承颜
2023-03-14
匿名用户

<files-path name="my_images" path="files/"/>

这指向getFilesDir()内的file/目录。但是,这不是createImageFile()要放置文件的地方。相反,它使用的是Environment.getExtranalStoragePublicDirectory(环境。DIRECTORY_PICTURES)。你需要:

>

  • 确定哪个位置是正确的(或者选择其他选项,而不是其中任何一个),然后

    同步实现

  • 宇文峰
    2023-03-14

    我曾经为文件提供程序的所有路径提供样板代码。你永远不会犯这样的错误。

    <?xml version="1.0" encoding="utf-8"?>
    <paths>
      <external-path name="external" path="." />
      <external-files-path name="external_files" path="." />
      <cache-path name="cache" path="." />
      <external-cache-path name="external_cache" path="." />
      <files-path name="files" path="." />
    </paths>
    

    有关更多详细信息,您可以检查FileProvider-指定可用的文件

    乐正镜
    2023-03-14

    有点晚了。。但我终于能够解决。

    根据文档:path组件仅对应于使用环境调用getExternalFilesDir()时返回的路径。目录图片

    所以使用

    getExternalFilesDir(Environment.DIRECTORY\u图片)

    而不是

    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
    

    您的路径应该是这样的:

    <?xml version="1.0" encoding="utf-8"?>
    <paths>
        <external-path name="InstructiveRide" path="Android/data/com.package.ride/files/Pictures"/>
    </paths>
    

    如果你在Picture目录下有一个子目录,请写这个。

    <?xml version="1.0" encoding="utf-8"?>
    <paths>
        <external-path name="InstructiveRide" path="Android/data/com.package.ride/files/Pictures/InstructiveRide/"/>
    </paths>
    

     类似资料:
    • 我试图通过Instagram的意图共享文件,但我意识到在API 24上,我不能只共享URI到其他应用程序没有给它的权限。 此后https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en在本教程中,我设置了提供程序并提供如下路径: 清单 注意:有“tool

    • 编辑:好的,我已经尝试了这些建议,并将其更改为getExternalFilesDir(),但仍然收到相同的错误。跳到底部显示“已编辑代码”的地方,查看它现在是什么。我还更改了它,这样屏幕截图将保存到图片目录,而不是创建一个新目录。(结束编辑) 我有一个android应用程序,其中包含一个recyclerview。我已经创建了一个按钮,该按钮将导出并创建recyclerview数据的PNG,将其保存

    • Java文件: java.lang.IllegalArgumentExcture:未能找到包含 /storage/emulated/0/Android/data/com.chandan.halo/files/Pictures/JPEG_20170216_233855_-96483920.jpg的配置根 第Uri photouURI=…行中出现错误。。。。。。 文件\u paths.xml mani

    • 构建基本的应用程序,并获得IllegalArgument异常:有一个按钮启动相机应用程序,我试图将图像保存到图片。 发现一些类似问题,但无法解决我的问题: Android:FileProvider IllegalArgumentException未能找到包含/data/data/**/files/Videos/final的已配置根目录。mp4 FileProvider“找不到配置的根目录”异常 下

    • 我尝试使用以下方法从URI获取路径: 当我尝试压缩位图时: 我得到这个错误:

    • 我试图发送使用tcp套接字的文件列表,但我得到这个文件提供商错误。谢啦 原因:java。lang.IllegalArgumentException:未能找到包含 文件路径 文件路径。xml manifest.xml: