Android SD卡写入,权限被拒绝
我正在尝试使用以下代码将文件写入SDCard(权限android.permission.WRITE_EXTERNAL_STORAGE
已在manifest.xml中设置)。 执行 nmea_file.createNewFile();
时,它会抛出 Permission Denied
异常。
任何猜测为什么会发生这种情况?
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
Log.d(TAG, "Sdcard was not mounted !!" );
}
else
{
File nmea_file;
File root = Environment.getExternalStorageDirectory();
FileWriter nmea_writer = null;
try {
nmea_file = new File(root,"NMEA.txt");
if(!nmea_file.exists()) {
Log.w(TAG, "File Doesn't Exists!");
nmea_file.createNewFile();
}
nmea_writer = new FileWriter(nmea_file);
nmea_writer.append(nmea);
nmea_writer.flush();
}
catch (IOException e)
{
Log.w(TAG, "Unable to write", e);
}
finally
{
if (nmea_writer != null)
{
try
{
nmea_writer.close();
}
catch (IOException e)
{
Log.w(TAG, "Exception closing file", e);
}
}
}
}
I am trying to write a file to SDCard with below Code (permission android.permission.WRITE_EXTERNAL_STORAGE
already set in manifest.xml).
Upon execution of nmea_file.createNewFile();
it throws exception with Permission Denied
.
Any guesses why would this be happening?
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
Log.d(TAG, "Sdcard was not mounted !!" );
}
else
{
File nmea_file;
File root = Environment.getExternalStorageDirectory();
FileWriter nmea_writer = null;
try {
nmea_file = new File(root,"NMEA.txt");
if(!nmea_file.exists()) {
Log.w(TAG, "File Doesn't Exists!");
nmea_file.createNewFile();
}
nmea_writer = new FileWriter(nmea_file);
nmea_writer.append(nmea);
nmea_writer.flush();
}
catch (IOException e)
{
Log.w(TAG, "Unable to write", e);
}
finally
{
if (nmea_writer != null)
{
try
{
nmea_writer.close();
}
catch (IOException e)
{
Log.w(TAG, "Exception closing file", e);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
添加到manifest.xml
Add to manifest.xml
如果 SD 卡被阻止执行某些操作,则可能会发生这种情况,例如:
It may happen if SD card is blocked for some operations, like:
您可能需要检查您是否有权访问 SDCARD。以下是在代码中执行此操作的方法:
You might want to check that you have access to SDCARD. Here is how you can do it in code:
请注意,uses-sdk 语句会影响您写入 SD 卡的能力(!)。
我的 AndroidManifest.xml 具有以下内容:
即使我没有声明 android.permission.WRITE_EXTERNAL_STORAGE,我也可以毫无问题地写入 SD 卡。
当我将 use-sdk 语句更改为:
我的所有 SD 卡写入均因权限被拒绝而失败!诚然应该声明 android.permission.WRITE_EXTERNAL_STORAGE ,但为什么一个 use-sdk 语句可以工作,而另一个却不能呢?
Be aware that your uses-sdk statement can effect you ability to write to the SD card(!).
My AndroidManifest.xml had the following:
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:
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?
如果您正在检查模拟器,请检查 SD 卡是否已安装。另外,不要忘记在创建模拟器时指定 SD 卡的大小。然后你需要添加
在你的清单中。
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.