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

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

葛承教
2023-03-14

编辑:好的,我已经尝试了这些建议,并将其更改为getExternalFilesDir(),但仍然收到相同的错误。跳到底部显示“已编辑代码”的地方,查看它现在是什么。我还更改了它,这样屏幕截图将保存到图片目录,而不是创建一个新目录。(结束编辑)

我有一个android应用程序,其中包含一个recyclerview。我已经创建了一个按钮,该按钮将导出并创建recyclerview数据的PNG,将其保存到设备,然后将其作为附件发送到电子邮件应用程序,以便通过电子邮件发送。我收到异常“java.lang.IllegalArgumentException:未能找到包含/storage/emulated/0/ExportedFlares/FlareData.png的配置根目录”

以下是将位图保存到设备的函数的代码:

 private void saveBitmap(Bitmap bitmap){
    if(bitmap!=null){
        try {
            FileOutputStream outputStream = null;
            try {
                String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
                        "/ExportedFlares";
                File dir = new File(file_path, "FlareData");
                if(!dir.exists())
                    dir.mkdirs();
                outputStream = new FileOutputStream(dir); //here is set your file path where you want to save or also here you can set file object directly

                bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream); // bitmap is your Bitmap instance, if you want to compress it you can compress reduce percentage
                // PNG is a lossless format, the compression factor (100) is ignored
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (outputStream != null) {
                        outputStream.flush();
                        outputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

以下是点击按钮时的onClick代码:

 public void onClick(View v) {
            saveBitmap(getScreenshotFromRecyclerView(recyclerView));

            String filename = "FlareData.png";
            File fileLocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
                    "/ExportedFlares", filename);
            Uri path = FileProvider.getUriForFile(FlareActivity.this, BuildConfig.APPLICATION_ID + ".provider",fileLocation);

            Intent emailIntent = new Intent(Intent.ACTION_SEND);
            emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            emailIntent .setType("vnd.android.cursor.dir/email");
            String to[] = {"email@gmail.com"};
            emailIntent .putExtra(Intent.EXTRA_EMAIL, to);

            emailIntent .putExtra(Intent.EXTRA_STREAM, path);

            emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
            startActivity(Intent.createChooser(emailIntent , "Send email..."));

以下代码的最后一行是引发异常的代码:

File fileLocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
                    "/ExportedFlares", filename);
            Uri path = FileProvider.getUriForFile(FlareActivity.this, BuildConfig.APPLICATION_ID + ".provider",fileLocation);

这里是XML数据,我这里有provider_paths.xml:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="FlareData.png" path="ExportedFlares/"/>
</paths>

这是舱单上的信息:

enter code here
        <provider
        android:name="androidx.core.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>

编辑代码:

emailFlaresButton.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            Context context = getApplicationContext();
            saveBitmap(context, getScreenshotFromRecyclerView(recyclerView));


                String filename = "FlareData.png";
                File fileLocation = new File(context.getExternalFilesDir(DIRECTORY_PICTURES).getAbsolutePath()
, filename);
                Uri path = FileProvider.getUriForFile(FlareActivity.this, BuildConfig.APPLICATION_ID + ".provider",fileLocation);

                Intent emailIntent = new Intent(Intent.ACTION_SEND);
                emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

                emailIntent .setType("vnd.android.cursor.dir/email");
                String to[] = {"asd@gmail.com"};
                emailIntent .putExtra(Intent.EXTRA_EMAIL, to);

                emailIntent .putExtra(Intent.EXTRA_STREAM, path);

                emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
                startActivity(Intent.createChooser(emailIntent , "Send 
                email..."));
                // Intent intent = new Intent(FlareActivity.this, 
                AddFlareActivity.class);
                //startActivityForResult(intent, ADD_FLARE_RESULT_CODE);
            }
        });

下面是错误指向的代码段(最后一行):

                Uri path = FileProvider.getUriForFile(FlareActivity.this, BuildConfig.APPLICATION_ID + ".provider",fileLocation);

我尝试过将提供者路径设置为外部文件路径和外部路径,但这不会影响问题

EDIT3:全堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.fibrnah, PID: 22052
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.android.fibrnah/files/Pictures/FlareData.png
    at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
    at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
    at com.android.fibrnah.FlareActivity$2.onClick(FlareActivity.java:84)
    at android.view.View.performClick(View.java:6274)
    at android.view.View$PerformClick.run(View.java:24859)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6710)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)

共有1个答案

锺离浩慨
2023-03-14

外部文件路径更改为外部路径

或者,将您的文件放在getExtranalFilesDir()中(在Context中调用)。这是更好的解决方案,因为您当前的代码将不适用于Android Q(默认情况下)和Android R(适用于所有应用程序)。

 类似资料: