Android - can save file in USB Device but not in Emulator

Viewed 78

I'm trying to transfer a file from PC to an android app.My code works when I use a USB device and I can see my sent file in Downloads. However I can't manage to do the same when using an emulator and it gives me the "ENOENT (No such file or directory) " exception.here is my code:

IncomingFileTransfer transfer = request.accept();
File path = Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_DOWNLOADS);
File file = new File(path, "/" +  transfer.getFileName());
try{
    transfer.recieveFile(file);//problem
    while(!transfer.isDone()) {
        try{
            Thread.sleep(1000L);
        }catch (Exception e) {
            e.printStackTrace();
        }
        if(transfer.getStatus().equals(FileTransfer.Status.error)) {
           System.out.println(transfer.getError());
        }
        if(transfer.getException() != null) {
            transfer.getException().printStackTrace();
        }
    }
    System.out.println("file received");
}catch (Exception e) {
    e.printStackTrace();
}
  • I use the following permission in manifest:

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

  • my minSdkVersion and targetSdkVersion are both 17

  • "protect sd card" option in my emulator is unchecked

  • here is my emulator settings

Thanks in advance!

0 Answers
Related