简体   繁体   中英

IO exception while creating file on android sdcard with permissions granted

I am trying to move file from internal phone memory to sd card.

val oldFile = File(oldPath) //oldPath is string
val newFile = File(newPath) //newPath is string

if(newFile.exists()) {
    newFile.delete()
}

newFile.parentFile.mkdirs()
newFile.createNewFile() //crashes here

// copy file code

Paths are absolute.

Old file path is:

/storage/emulated/0/Android/data/com.package/files/2ec37ce3-ca72-4a35-a6e6-2d7f8e864c6c

New file path is:

/storage/48EE-C144/chosenDirectory/2ec37ce3-ca72-4a35-a6e6-2d7f8e864c6c

where 2ec37ce3-ca72-4a35-a6e6-2d7f8e864c6c is file.

Paths are proper and the new file path is chosen manually by user with external directory picker library which works fine.

Of course app asks user for permissions and does not shall pass user to app until permissions are not granted.

int perm = this.checkSelfPermission(permission.WRITE_EXTERNAL_STORAGE);
int perm2 = this.checkSelfPermission(permission.READ_EXTERNAL_STORAGE);

if (perm != PackageManager.PERMISSION_GRANTED || perm2 != PackageManager.PERMISSION_GRANTED) {
    /*ask for permissions and show explain dialog*/
}

Parmissions in manifest are added too.

Application crashes while calling newFile.createNewFile() with exception log:

05-07 13:02:14.995 14004-14004/com.packagename E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.packagename, PID: 14004
    java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451) 
     Caused by: java.io.IOException: Permission denied
        at java.io.UnixFileSystem.createFileExclusively0(Native Method)
        at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
        at java.io.File.createNewFile(File.java:948)
        at com.packagename.backend.localApi.changePath.PathChanger.changePath(PathChanger.kt:29)
        at com.packagename.backend.localApi.changePath.PathChangeService$changePath$1.onSelect(PathChangeService.kt:49)
        at com.codekidlabs.storagechooser.fragments.ChooserDialogFragment$1.onItemClick(ChooserDialogFragment.java:169)
        at android.widget.AdapterView.performItemClick(AdapterView.java:343)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1665)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:4075)
        at android.widget.AbsListView$10.run(AbsListView.java:6552)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6816)
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451) 

需要在AndroidManifest.xml文件中添加权限。

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

我的解决方案是停止使用 java 类并迁移到存储访问框架

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM