【问题标题】:store data text to SD Card in android?在android中将数据文本存储到SD卡?
【发布时间】:2012-04-01 08:14:04
【问题描述】:

我必须将数据文本存储到 SD 卡。

这是我的代码:

 try {

                File myFile = new File(Environment.getExternalStorageDirectory()+"/mnt/sdcard/mysdfile.txt");

                myFile.createNewFile();
                FileOutputStream fOut = new FileOutputStream(myFile);
                OutputStreamWriter myOutWriter = 
                                        new OutputStreamWriter(fOut);
                myOutWriter.append(txtData.getText());
                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();
            }

在 AndroidMainfest 我有:

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

我不明白为什么它不起作用?

在 Toast 中报错 :Permission denied?

请帮帮我。

【问题讨论】:

标签: android sd-card


【解决方案1】:

您也可以试试这个https://github.com/uberspot/AndroidStorageUtils,它是一个包装类/包,可以让android 中的存储使用更容易一些。 :) 它还有一个“saveStringOnExternalStorage”方法。

【讨论】:

    【解决方案2】:

    试试这个代码一定能解决问题...

    try{
        String filename = "filename.txt";
        File myFile = new File(Environment.getExternalStorageDirectory(), filename);
    
        if(!myFile.exists()) 
            myFile.createNewFile();
        FileOutputStream fos;
        byte[] data = txtData.getBytes();
        try {
            fos = new FileOutputStream(myFile);
            fos.write(data);
            fos.flush();
            fos.close();
        } 
        catch (FileNotFoundException e) {
        // handle exception
      } catch (IOException e) {
        // handle exception
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-12
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      • 1970-01-01
      • 2010-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多