2

I am trying to copy a file from my app to USB Drive(Pen drive) in Android but I am getting the Permission denied.

I have already given runtime storage permission.

Process: com.testapplication, PID: 19261 java.io.FileNotFoundException: /mnt/runtime/write/02EE-0EE1/Tele-calling list.xlsx (Permission denied) at java.io.FileOutputStream.open0(Native Method)

Manifest

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

Code

val cacheFile = File(cachepath, it.name)
val pendriveFile = File(pendrivepath, it.name)
Log.d(TAG, "********: " + cacheFile.path)
Log.d(TAG, "&&&&&&& " + pendriveFile.path)
cacheFile.copyTo(pendriveFile)
Toast.makeText(context, it.name + " copying..", Toast.LENGTH_SHORT).show()

I am facing issue in cacheFile.copyTo(pendriveFile) line. Pendrive path is "/storage/02EE-0EE1" that I have calculated in runtime.

Son Truong
  • 13,661
  • 5
  • 32
  • 58
Rahul Giradkar
  • 1,818
  • 1
  • 17
  • 28
  • Does this answer your question? [How do you write to a folder on an SD card in Android?](https://stackoverflow.com/questions/3551821/how-do-you-write-to-a-folder-on-an-sd-card-in-android) – JensV Nov 30 '20 at 17:36
  • `Pendrive path is "/storage/02EE-0EE1"` Then why are you using a different path? Further the drive is read only just as a removable micro sd card. Well.. under Android. You can only write to an app specific directory on them. – blackapps Nov 30 '20 at 18:02
  • "/storage/02EE-0EE1" ,This is path of my pedrive. When I attach to it. – Rahul Giradkar Dec 01 '20 at 06:02
  • I tried with "/mnt/runtime/write/02EE-0EE1/" this path too but same error – Rahul Giradkar Dec 01 '20 at 06:11
  • 1
    Nowadays, you have to use the Android Storage Access Framework (SAF) to access any external files/folders by obtaining the correct context before performing any standard programming file operations. Starting from API29, you also need to implement the Scoped Storage approach. – ecle Dec 01 '20 at 11:29

0 Answers0