无法写入SD卡上的Andr​​oid手机 [英] Cannot write to SD Card on Android phone

查看:175
本文介绍了无法写入SD卡上的Andr​​oid手机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EDIT2:对于整个人从谷歌这个问题的绊脚石。该 canWrite()函数是有点越野车,似乎并没有正常工作。我建议不使用它。只要和异常,而不是。另外,还要确保你的手机是不是在 USB存储模式,这样就可以写入SD卡。最后要确保在Android设备的设置不具备的那种对写保护什么的。

我似乎有试图写我的SD卡在我的Andr​​oid手机的问题。没关系,如果我在调试模式下使用Eclipse,或运行在它自己的手机。我不能写入SD卡。是的权限配置,如下图所示。当使用提供了对Android的网站上的code段我被虚报为两个布尔它完成后段。我绝对不知道为什么存储不可写的。我弹出SD卡和手机,从我的Mac,以确保他们没有使用它,手机可以安装,但在不使用Eclipse都像我说的,我得到了同样的问题。

编辑:使用真正的Andr​​oid手机会根据有关的信息运行4.04

 布尔mExternalStorageAvailable = FALSE;
布尔mExternalStorageWriteable = FALSE;
字符串状态= Environment.getExternalStorageState();

如果(Environment.MEDIA_MOUNTED.equals(州)){
    //我们可以读取和写入的媒体
    mExternalStorageAvailable = mExternalStorageWriteable = TRUE;
}否则,如果(Environment.MEDIA_MOUNTED_READ_ONLY.equals(州)){
    //我们只能读取介质
    mExternalStorageAvailable = TRUE;
    mExternalStorageWriteable = FALSE;
} 其他 {
    //别的东西是错误的。这可能是很多其他的国家之一,但我们需要
    //知道的是,我们既不能读也不能写
    mExternalStorageAvailable = mExternalStorageWriteable = FALSE;
}
 

清单:

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.tinywebteam.gpstracker
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <使用-权限的Andr​​oid:名称=android.permission.ACCESS_COARSE_LOCATION/>
    <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
    <使用-权限的Andr​​oid:名称=android.permission.READ_EXTERNAL_STORAG​​E/>
    <使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>
    <用途-SDK
        安卓的minSdkVersion =8
        机器人:targetSdkVersion =17/>

    <应用
        机器人:allowBackup =真
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名称=com.tinywebteam.gpstracker.MainActivity
            机器人:标签=@字符串/ APP_NAME>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>

                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>

< /舱单>
 

它试图执行失败时 mkdirs()下方,并引发指定的异常:

 尝试{
            文件SD卡= Environment.getExternalStorageDirectory();
            文件DIR =新的文件(sdCard.getAbsolutePath()+/ GPStracker /数据/);
            如果(!dir.exists()){
                如果(!dir.mkdirs())
                    抛出新的FileNotFoundException异常(无法创建目录。);
            }
            文件FILEOUT =新的文件(目录,GPSTracker_+ TS +名为.csv);
            如果(fileOut.canWrite()){
                FileOutputStream中出=新的FileOutputStream(FILEOUT);
                out.write(fileStr.getBytes());
                out.close();
                的System.out.println(文件写入成功);
            } 其他 {
                的System.out.println(无法写入文件);
            }
        }赶上(FileNotFoundException异常E){
            e.printStackTrace();
        }赶上(IOException异常E){
            e.printStackTrace();
        }
 

解决方案

根据该的文档,你所描述的手段的状态下,外部介质的存储不能访问你:

  

公共静态最后弦乐MEDIA_SHARED

     

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

     

常数值:共享

您需要去您的USB大容量存储选项,并关闭USB存储器。

EDIT2: For people stumbling across this question from Google. The canWrite() function is kinda buggy and doesn't seem to work properly. I would recommend not using it. Just catch and exceptions instead. Also make sure your phone is not in USB Storage mode so that you can write to the SD card. Lastly make sure the settings in Android device don't have write protection or anything of the sort on.

I seem to be having issues trying to write to my SD card on my Android phone. It doesn't matter if I am in debug mode with Eclipse, or running the phone on it's own. I cannot write to the SD Card. Yes the permissions are configured, as shown below. When using the code snippet provided on the Android site I am being given false for both of the booleans after it finishes the segment. I have absolutely no idea why the storage is not available or writable. I eject the SD card and the phone from my Mac to make sure they are not using it and that the phone can mount, but like I said I get the same issue when not using Eclipse at all.

EDIT: Using a real Android phone running 4.04 according to the about information.

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

Manifest:

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

It fails when trying to execute mkdirs() below and throws the assigned exception:

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

解决方案

According to the documentation, the state you're describing means that the external media storage isn't accessible to you:

public static final String MEDIA_SHARED

Added in API level 1 getExternalStorageState() returns MEDIA_SHARED if the media is present not mounted, and shared via USB mass storage.

Constant Value: "shared"

You need to go to your USB Mass Storage options and turn off USB storage.

这篇关于无法写入SD卡上的Andr​​oid手机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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