0

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.

            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 :

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 ?

Ahriman
  • 115
  • 8
  • Your `FileProvider` metadata does not cover `Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)`, apparently. – CommonsWare Oct 29 '19 at 21:14
  • Thank you. I didn't check it since a while but you're right, it was pointing on another folder... So now, this part is working. Unfortunately, if the folder doesn't exist, the app doesn't create it. It creates a file with no extension in the MUSIC folder. How can we specify that I would like a folder ? – Ahriman Oct 30 '19 at 09:38
  • ok, found it. I have to use mkdir() and not .createNewFile. Thank's for your help. – Ahriman Oct 30 '19 at 09:45

1 Answers1

0

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. 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. It's necessarry to use .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? Current code is deleting the that folder

Ahriman
  • 115
  • 8