Android SD card writing, Permission Denied

久未见 提交于 2019-11-27 21:19:53

Add to manifest.xml

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

It may happen if SD card is blocked for some operations, like:

  1. Preparing to dismount SD card from slot
  2. Device connected to PC as external USB drive

You might want to check that you have access to SDCARD. Here is how you can do it in code:

if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
    Toast.makeText(this, "External SD card not mounted", Toast.LENGTH_LONG).show();
}

Be aware that your uses-sdk statement can effect you ability to write to the SD card(!).

My AndroidManifest.xml had the following:

<uses-sdk minSdkVersion="8"/>

And I could write to the SD card without any problems even though I had not declared android.permission.WRITE_EXTERNAL_STORAGE.

When I changed my uses-sdk statement to:

<uses-sdk android:targetSdkVersion="9" minSdkVersion="8" />

All my SD card writes failed with a permission denied! Granted that android.permission.WRITE_EXTERNAL_STORAGE should have been declared, but why with one uses-sdk statement it worked and the other it did not?

Check whether sdcard is mounted or not if you are checking in emulator. Also dont foget to give some size for sdcard at the time of creating the emulator. Then you need to add
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in your manifest.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!