3

EDIT2:对于那些从谷歌偶然发现这个问题的人。该canWrite()功能有点错误,似乎无法正常工作。我建议不要使用它。只是catch和例外。还要确保您的手机未处于USB Storage模式,以便您可以写入 SD 卡。最后确保 Android 设备中的设置没有写保护或类似的东西。

我似乎在尝试在我的 Android 手机上写入我的 SD 卡时遇到问题。我是在 Eclipse 的调试模式下,还是自己运行手机都没有关系。我无法写入 SD 卡。是的,权限已配置,如下所示。使用 Android 网站上提供的代码片段时,在完成该段后,我的两个布尔值都为 false。我完全不知道为什么存储不可用或不可写。我从我的 Mac 中弹出 SD 卡和手机,以确保它们没有使用它并且手机可以安装,但就像我说的那样,我在完全不使用 Eclipse 时遇到了同样的问题。

编辑:根据关于信息使用运行 4.04 的真正 Android 手机。

boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {
    // We can read and write the media
    mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    // We can only read the media
    mExternalStorageAvailable = true;
    mExternalStorageWriteable = false;
} else {
    // Something else is wrong. It may be one of many other states, but all we need
    //  to know is we can neither read nor write
    mExternalStorageAvailable = mExternalStorageWriteable = false;
}

显现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tinywebteam.gpstracker"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.tinywebteam.gpstracker.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

尝试在mkdirs()下面执行时失败并抛出分配的异常:

try {
            File sdCard = Environment.getExternalStorageDirectory();
            File dir = new File(sdCard.getAbsolutePath() + "/GPStracker/data/");
            if (!dir.exists()) {
                if (!dir.mkdirs())
                    throw new FileNotFoundException("Couldn't make directory.");
            }
            File fileOut = new File(dir, "GPSTracker_" + ts + ".csv");
            if (fileOut.canWrite()) {
                FileOutputStream out = new FileOutputStream(fileOut);
                out.write(fileStr.getBytes());
                out.close();
                System.out.println("File written successfully");
            } else {
                System.out.println("Could not write to file");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
4

3 回答 3

2

根据文档,您所描述的状态意味着您无法访问外部媒体存储:

公共静态最终字符串 MEDIA_SHARED

在 API 级别 1 中添加 getExternalStorageState() 如果媒体存在但未安装,则返回 MEDIA_SHARED,并通过 USB 大容量存储共享。

常数值:“共享”

您需要转到 USB 大容量存储选项并关闭 USB 存储。

于 2013-04-04T19:48:07.723 回答
0

这是交易,你正在使用

File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/GPStracker/data/");

而是使用 -

File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdcard + "/GPStracker/data");

在这里,我们没有使用第二个“/”,因为mkdirs()将其误解为创建递归目录。你不会得到这个例外。并检查您传递的文件名是否在前面附加了“/”,或者它也以这种方式工作。

于 2013-04-04T20:03:32.400 回答
0

我有一个三星 Galaxy s3(运行 android 4.1.2),我的内部存储器名为 sdCard0,我的外部 sd 卡名为 extSdCard。

所以 Environment.getExternalStorageDirectory() 返回了 sdCard0 的路径,我的内部手机内存

在这种情况下,您可以使用以下内容获取实际路径。

String externalpath = new String();
String internalpath = new String();

public  void getExternalMounts() {
Runtime runtime = Runtime.getRuntime();
try
{
Process proc = runtime.exec("mount");
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
String line;

BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
    if (line.contains("secure")) continue;
    if (line.contains("asec")) continue;

    if (line.contains("fat")) {//external card
        String columns[] = line.split(" ");
        if (columns != null && columns.length > 1) {
            externalpath = externalpath.concat("*" + columns[1] + "\n");
        }
} 
        else if (line.contains("fuse")) {//internal storage
        String columns[] = line.split(" ");
        if (columns != null && columns.length > 1) {
            internalpath = internalpath.concat(columns[1] + "\n");
        }
    }
}
}
catch(Exception e)
{
    e.printStackTrace();
}
System.out.println("Path  of sd card external............"+externalpath);
System.out.println("Path  of internal memory............"+internalpath);
}

获得实际路径后,您可以将文件写入 sdcard。

于 2013-04-04T20:05:05.107 回答