Search code examples
javaandroidfileoutputstreambufferedinputstream

Saving Stream to Folder - fail readDirectory() errno=20


I am trying to save a stream to a subfolder of storage/emulated/0, but am getting the error fail readDirectory() errno=20

I am using the below code :

BufferedInputStream bis = new BufferedInputStream(instream, buffersize);
BufferedOutputStream out = new BufferedOutputStream(
    new FileOutputStream(fileName + ".download"),
    buffersize);
                
int len;
int downloadedlen = 0;
byte[] buff = new byte[buffersize];
String firstfewchars = null;
while ((len = bis.read(buff)) > 0) {
    Log.i(TAG, "Writing Data from Stream Line 814");
    out.write(buff, 0, len);
}

Can anyone suggest what is wrong ?

Thanks.


Solution

  • Are you sure you have permissions, and folder is public?

    For testing, You can try get directory in this way:

    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
    

    Also, don't forget permissions in manifest:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>