简体   繁体   English

如果文件夹不存在,则创建一个文件夹,然后创建一个文件并共享它

[英]Create a folder if it doesn't exist, then create a file and share it

I have an issue with my sharing option.我的共享选项有问题。 The option was perfectly well working (and I have saved it before trying some modifications) but I have tried to modify something and I cannot reach my goal).该选项运行良好(我在尝试进行一些修改之前已将其保存)但我尝试修改某些内容但无法达到我的目标)。

The purpose is: click on the option in a menu, click on share, if the folder "test folder" doesn't exists in the "MUSIC" folder, create it, if it already exists, copie the sound, put it in the folder previously created and then use it as an extra in a SEND intent.目的是:点击菜单中的选项,点击分享,如果“MUSIC”文件夹中不存在“test文件夹”文件夹,则创建,如果已经存在,复制声音,放入之前创建的文件夹,然后将其用作发送意图中的额外内容。

            case R.id.share:
            if (isStoragePermissionGranted()) {
                File outputFile = new File("");
                InputStream is = getResources().openRawResource(((Sound) adapter.getItem(index)).getMpsound());
                try {
                    File outputDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC), "Test folder");
                    outputFile = new File(outputDir, getResources().getResourceEntryName(((Sound) adapter.getItem(index)).getMpsound()) + ".mp3");

                    if (outputDir.exists()) {
                        byte[] buffer = new byte[is.available()];
                        is.read(buffer);
                        OutputStream os = new FileOutputStream(outputFile);
                        os.write(buffer);

                        Intent share = new Intent(Intent.ACTION_SEND);
                        share.setType("audio/*");
                        share.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", outputFile));
                        share.putExtra(Intent.EXTRA_TEXT, "\"" + ((Sound) adapter.getItem(index)).getTitle_show() + "\" shared by my app");
                        startActivity(Intent.createChooser(share, "Share Sound File"));
                    } else {
                        outputDir.createNewFile(); //plus add code as below to share the sound after creating the folder
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

I'm not able to create the folder and if I create it manually, the code is working well because the mp3 appears in the folder, and right after I have an error that says:我无法创建文件夹,如果我手动创建它,代码运行良好,因为 mp3 出现在文件夹中,并且在我遇到错误之后:

java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Music/my app/sound_test.mp3

So... the app is able to access the folder to create the sound but for an unknown reason, I have this issue.所以......该应用程序能够访问文件夹以创建声音,但由于未知原因,我遇到了这个问题。

What did I do wrong?我做错了什么?

So, for the ones who are coming in few days, months, years with the same problem:所以,对于那些在几天、几个月、几年内遇到同样问题的人:

As CommonsWare said it, the issue in accessing the folder was from the FileProvider .正如 CommonsWare 所说,访问文件夹的问题来自FileProvider After configure it properly with the good path, I had no more issue.用好的路径正确配置后,我没有更多问题了。

For the part concerning the creation of a new folder, .createNewFile() doesn't seems to be the right way.对于有关创建新文件夹的部分, .createNewFile()似乎不是正确的方法。 It's necessarry to use .mkdir()必须使用.mkdir()

And to end it, the part concerning the delete of the files in the folder, you will have your answer there:最后,关于删除文件夹中文件的部分,您将在那里得到答案:

File dir = new File(Environment.getExternalStorageDirectory()+"Dir_name_here"); 
if (dir.isDirectory()) 
{
    String[] children = dir.list();
    for (int i = 0; i < children.length; i++)
    {
       new File(dir, children[i]).delete();
    }
}

Source: How to delete all files in a directory java android app?来源: 如何删除目录 java android app 中的所有文件? Current code is deleting the that folder 当前代码正在删除该文件夹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM