写权限到SD卡 [英] Permission to write to the SD card
问题描述
我想我的应用程序归档应用程序数据库到SD卡。在我的code我检查目录 canWrite()
的存在,如果没有,则抛出一个 IOException异常
。在这种特定情况下,我想该数据库文件复制到SD卡的根目录下,但它抛出一个 IOException异常
。如何更改权限的文件夹/文件要能写呢?
I would like my app to archive the application DB to the SD card. In my code I check if the directory canWrite()
exists, and if not, throw an IOException
. In this particular instance, I am trying to copy the db file to the root directory on the SD card, but it's throwing an IOException
. How can I change the permission on a folder/file to be able to write to it?
推荐答案
您说得对,SD卡目录是 / SD卡
,但你不应该是硬编码它。相反,打电话到<一个href="http://developer.android.com/intl/fr/reference/android/os/Environment.html#getExternalStorageDirectory%28%29"><$c$c>Environment.getExternalStorageDirectory()$c$c>得到的目录:
You're right that the SD Card directory is /sdcard
but you shouldn't be hard coding it. Instead, make a call to Environment.getExternalStorageDirectory()
to get the directory:
File sdDir = Environment.getExternalStorageDirectory();
如果您还没有这样做的话,你就需要通过添加下面的清单中的线,让您的应用程序写入SD卡正确的权限:
If you haven't done so already, you will need to give your app the correct permission to write to the SD Card by adding the line below to your Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
这篇关于写权限到SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!