grantespo
grantespo

Reputation: 2269

Android Mp3 File - open failed: ENOENT (No such file or directory)

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

     }
   }
 }

Upvotes: 0

Views: 881

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006819

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.

Upvotes: 1

Related Questions