简体   繁体   English

在Android中将文件保存到SD卡时出现问题

[英]Problems saving a file to sdcard in android

I am trying to get a file to save to my sdcard. 我试图获取一个文件保存到我的SD卡。 Here is the code I have so far. 这是我到目前为止的代码。 I really appreciate any help you can offer. 我非常感谢您可以提供的任何帮助。 Thanks Edit: The problem is that the file is not saved. 谢谢编辑:问题是该文件没有保存。 When I exit the program and look at the sdcard no file was saved. 当我退出程序并查看sdcard时,没有文件被保存。 I put a toast into the error catchers and it looks like I'm getting an IOException. 我向错误捕获器敬酒,看起来我正在获得IOException。 I don't know how to check the StackTrace log though 我不知道如何检查StackTrace日志

class bSaveListen implements Button.OnClickListener{
    @Override

    public void onClick(View arg0) {
        savef();
    }

}

public void savef(){
    sdCard = Environment.getExternalStorageDirectory();
    String filename = "aaaaaaa.txt";
    file = new File(sdCard, filename);
    FileOutputStream fileout;
    byte[] thatString = new String("awesome string").getBytes();
    try {
        fileout = new FileOutputStream(file);
        fileout.write(thatString);
        fileout.flush();
        fileout.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Probably you are getting an exception, you simply are not seeing it. 可能您正在获得例外,而您根本没有看到它。 Instead of e.printStackTrace(); 代替e.printStackTrace(); , which is useless since it doesn't show up in DDMS, use Le(TAG, "Error", e); ,它没有用,因为它没有显示在DDMS中,请使用Le(TAG, "Error", e); That way you should be able to see the Exception and figure out what's wrong 这样一来,您应该能够看到Exception并找出问题所在

I spent all day today trying to figure this out and I finally have an answer. 我今天花了一整天的时间试图弄清楚这一点,终于有了答案。 Just thought I would post it here in case anyone else is having a similar issue. 只是以为我会在这里发布,以防其他人遇到类似问题。 I stupidly wrote my permission inside the application block of code instead of below it. 我愚蠢地在应用程序代码块内而不是在代码下面写了我的许可。 Should be like this 应该是这样的

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".Main"
              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>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Not like this 不是这样

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".Main"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</application>`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM