Android SD卡写入,权限被拒绝

发布于 2024-10-08 17:57:32 字数 1184 浏览 7 评论 0原文

我正在尝试使用以下代码将文件写入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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

莫多说 2024-10-15 17:57:32

添加到manifest.xml

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

Add to manifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
睫毛上残留的泪 2024-10-15 17:57:32

如果 SD 卡被阻止执行某些操作,则可能会发生这种情况,例如:

  1. 准备从插槽中卸下 SD 卡
  2. 作为外部 USB 驱动器连接到 PC 的设备

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
残龙傲雪 2024-10-15 17:57:32

您可能需要检查您是否有权访问 SDCARD。以下是在代码中执行此操作的方法:

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

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();
}
怪我太投入 2024-10-15 17:57:32

请注意,uses-sdk 语句会影响您写入 SD 卡的能力(!)。

我的 AndroidManifest.xml 具有以下内容:

<uses-sdk minSdkVersion="8"/>

即使我没有声明 android.permission.WRITE_EXTERNAL_STORAGE,我也可以毫无问题地写入 SD 卡。

当我将 use-sdk 语句更改为:

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

我的所有 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:

<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?

狼性发作 2024-10-15 17:57:32

如果您正在检查模拟器,请检查 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文