允许写入 SD 卡 [英] Permission to write to the SD card

查看:35
本文介绍了允许写入 SD 卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的应用程序将应用程序数据库存档到 SD 卡.在我的代码中,我检查目录 canWrite() 是否存在,如果不存在,则抛出一个 IOException.在这个特定的例子中,我试图将 db 文件复制到 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 卡目录是 /sdcard 是对的,但您不应该对其进行硬编码.相反,调用Environment.getExternalStorageDirectory() 获取目录:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆