【问题标题】:Create a folder if it doesn't exist, then create a file and share it如果文件夹不存在,则创建一个文件夹,然后创建一个文件并共享它
【发布时间】:2019-10-29 17:55:33
【问题描述】:

我的分享选项有问题。 该选项运行良好(我在尝试进行一些修改之前已将其保存)但我尝试修改某些内容但无法达到我的目标)。

目的是:点击菜单中的选项,点击分享,如果“MUSIC”文件夹中不存在“test文件夹”文件夹,则创建,如果已经存在,复制声音,放入它在之前创建的文件夹中,然后将其用作 SEND 意图中的额外内容。

            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();
                }
            }

我无法创建文件夹,如果我手动创建它,代码运行良好,因为 mp3 出现在文件夹中,并且在我出现错误之后:

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

所以...应用程序能够访问文件夹以创建声音,但由于未知原因,我遇到了这个问题。

我做错了什么?

【问题讨论】:

  • 您的FileProvider 元数据显然不包括Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)。
  • 谢谢。我有一段时间没有检查它,但你是对的,它指向另一个文件夹......所以现在,这部分正在工作。不幸的是,如果该文件夹不存在,则应用程序不会创建它。它会在 MUSIC 文件夹中创建一个没有扩展名的文件。我们如何指定我想要一个文件夹?
  • 好的,找到了。我必须使用 mkdir() 而不是 .createNewFile。感谢您的帮助。

标签: android directory


【解决方案1】:

所以,对于那些在几天、几个月、几年后遇到同样问题的人:

正如 CommonsWare 所说,访问该文件夹的问题来自 FileProvider。用好路径正确配置后,我没有更多问题了。

关于创建新文件夹的部分,.createNewFile() 似乎不是正确的方法。必须使用.mkdir()

最后,关于删除文件夹中文件的部分,您将在那里得到答案:

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();
    }
}

来源:How to delete all files in a directory java android app? Current code is deleting the that folder

【讨论】:

    猜你喜欢
    • 2011-01-19
    • 2013-09-18
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 2014-09-07
    • 2018-02-10
    相关资源
    最近更新 更多