【问题标题】:System app is not saving text file on SD card系统应用程序未在 SD 卡上保存文本文件
【发布时间】:2015-10-09 13:01:38
【问题描述】:

我创建了一个系统应用程序,我试图在我的 SD 卡上保存一个文本文件,我编写了以下代码:

     Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

        if(isSDPresent)
        {

            try {
                File myFile = new File("/sdcard/mysdfile.txt");
                myFile.createNewFile();
                FileOutputStream fOut = new FileOutputStream(myFile);
                OutputStreamWriter myOutWriter =
                        new OutputStreamWriter(fOut);
                myOutWriter.append("tttttttteeeeeest");
                myOutWriter.close();
                fOut.close();
                 Toast.makeText(getBaseContext(),
                       "Done writing SD 'mysdfile.txt'",
                     Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                 Toast.makeText(getBaseContext(), e.getMessage(),
                    Toast.LENGTH_SHORT).show();
            }//

        }
        else
        {
            Toast.makeText(getBaseContext(),"There is no SD Card",Toast.LENGTH_LONG).show();
        }

它给了我以下错误:

open failed eacces (permission denied)

这是清单文件的开头:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test.myapplication"
    android:sharedUserId="android.uid.system"
    >

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

有什么帮助吗?

【问题讨论】:

  • 首先,从不硬编码路径。始终使用方法来导出路径(例如,getExternalFilesDir())。其次,在大多数 Android 设备上,/mnt/sdcard 不是 removable storage。第三,权限是READ_EXTERNAL_STORAGE,而不是read_external_storage。第四,如果您的targetSdkVersion 为 23 或更高,并且您正在 Android 6.0 及更高版本上进行测试,those permissions are dangerous and have to be requested at runtime
  • 发布您的完整 AndroidManifest,或者至少是它的开头,以便我们确定您的问题是什么。另外,阅读链接 CommonsWare 发布。

标签: android


【解决方案1】:

该应用是系统应用,因此您无权访问SD。

在这种情况下,您必须创建另一个具有 SD 权限的应用,并使用意图调用来读取/写入 SD。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多