0

I'm not understanding why I am getting this exception. The directory itself seems to be there. I can play the music locally, but when I try to do this: FileInputStream inputStream = new FileInputStream(audioFile); I get this error: /document/2710: open failed: ENOENT (No such file or directory)

Here is my full Code:

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 0 && resultCode == Activity.RESULT_OK){
        if ((data != null) && (data.getData() != null)) {

      Uri audioFileUri = data.getData();
      File file = new File(audioFileUri.getPath());
      //file.mkdirs();  <---- Making a directory does not work
      try {
             FileInputStream inputStream = new FileInputStream(audioFile); //This is where I get the error
           }
      catch{
             Log.i("TAG", e.getMessage());  
            }

     }
   }
 }
grantespo
  • 2,233
  • 2
  • 24
  • 62
  • Check programatically if the file exists and you have correct permissions: https://stackoverflow.com/questions/1816673/how-do-i-check-if-a-file-exists-in-java – Zakir Jun 08 '17 at 16:58

1 Answers1

1
File file = new File(audioFileUri.getPath());

At best, that line might work if the scheme of the Uri is file. In your case, it is content.

Use getContentResolver().openInputStream() to open an InputStream on the content identified by the Uri. This works for both the file and the content scheme.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Your answer in a different post actually solved my problem. I changed `FileInputStream inputStream = new FileInputStream(audioFile);` to `InputStream inputStream = getContentResolver().openInputStream(uri);` and now it works like a charm. – grantespo Jun 08 '17 at 17:10