Android SD 卡写入,权限被拒绝 [英] Android SD card writing, Permission Denied

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

问题描述

我正在尝试使用以下代码将文件写入 SDCard(权限 android.permission.WRITE_EXTERNAL_STORAGE 已在 manifest.xml 中设置).在执行 nmea_file.createNewFile(); 时,它会抛出异常,Permission Denied.

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);
            }
        }
    }
  }

推荐答案

添加到 manifest.xml

Add to manifest.xml

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

这篇关于Android SD 卡写入,权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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