2010-03-16 72 views
18

我想写一个文件从一个Http post回复到一个文件sdcard。一切工作正常,直到数据的字节数组被检索。写一个文件到SD卡

我试过在清单 中设置WRITE_EXTERNAL_STORAGE权限,并尝试了许多我在网上找到的教程的不同组合。

所有我能找到的是使用openFileOutput("",MODE_WORLD_READABLE)方法,活动的,但怎么我的应用程序写入的文件是通过使用一个线程。具体来说,当一个文件必须被写入时,从另一个线程调用一个线程,因此即使我尝试了一个活动对象也不起作用。

的应用程序已经走过了漫长的道路,我不能改变应用程序是如何当前写入。

请别人帮我吗?


CODE

File file = new File(bgdmanip.savLocation); 
FileOutputStream filecon = null; 
filecon = new FileOutputStream(file); 

byte[] myByte; 
myByte = Base64Coder.decode(seReply); 

bos.write(myByte); 
filecon.write(myByte); 
myvals = x * 11024; 

bgdmanip.savLocation拥有整个文件路径。 seReply是来自HttpPost响应的字符串答复。第二组代码循环参考x。该文件已创建,但仍为0字节。

回答

20

openFileOutput()方法写入数据到应用程序的私有数据区(不是SD卡),所以这可能不是你想要的。您应该能够拨打Environment.getExternalStorageDirectory()以获取SD卡的根路径并使用它创建一个FileOutputStream。从那里,只需使用标准的java.io例程。

+0

谢谢埃里希。我像这个“/sdcard/filename.ext”那样以字符串的形式传递完整路径。该文件也已创建,但文件大小仍为0字节。另外我不能在eclipse中使用DDMS的推/拉文件控件。 – 2010-03-16 14:47:01

+0

您可能需要发布一些文件编写代码来帮助诊断问题。并非全部,但至少是发生了什么的基础知识。另外,您是否在LogCat中看到任何消息? – 2010-03-16 15:04:08

+0

埃里克,我已经更新了代码。 没有关于LogCat中文件操作的消息。我改变了很多代码,在代码中出现错误,但不记得哪些代码给了我错误。 – 2010-03-17 04:07:16

3

这里有一个例子:

// Log used as debug 
File log = new File(Environment.getExternalStorageDirectory(), "Log.txt"); 
try { 
    out = new BufferedWriter(new FileWriter(log.getAbsolutePath(), false)); 
    out.write(new Date().toString()); 
    out.write(" : \n"); 
} catch (Exception e) { 
    Log.e(TAG, "Error opening Log.", e); 
} 
28
//------------------------------WRITING DATA TO THE FILE ---------------------------------  

btnWriteSDFile.setOnClickListener(new OnClickListener() 
    { 
    public void onClick(View v) 
    {  

     try { 
      File myFile = new File("/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(v.getContext(),"Done writing SD 'mysdfile.txt'", Toast.LENGTH_SHORT).show(); 
      txtData.setText(""); 
     } 
     catch (Exception e) 
     { 
      Toast.makeText(v.getContext(), e.getMessage(),Toast.LENGTH_SHORT).show(); 
     } 
    } 


    }); 

//---------------------------READING DATA FROM THE FILE PLACED IN SDCARD-------------------//  
     btnReadSDFile.setOnClickListener(new OnClickListener() 
     { 

     public void onClick(View v) 
     { 

     try { 

      File myFile = new File("/sdcard/mysdfile.txt"); 
      FileInputStream fIn = new FileInputStream(myFile); 
      BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn)); 
      String aDataRow = ""; 
      String aBuffer = ""; 
      while ((aDataRow = myReader.readLine()) != null) 
      { 
       aBuffer += aDataRow ; 
      } 
      txtData.setText(aBuffer); 
      myReader.close(); 
      Toast.makeText(v.getContext(),"Done reading SD 'mysdfile.txt'",Toast.LENGTH_SHORT).show(); 
     } 
     catch (Exception e) 
     { 
      Toast.makeText(v.getContext(), e.getMessage(),Toast.LENGTH_SHORT).show(); 
     } 
     } 
     }); 

随着这也写不出这个权限在Android.Manifest

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

多数民众赞成在我们这里称为程序员的看法 – 2013-06-29 19:12:36

+0

而不是edittext我想传递两个字符串值并将它们存储在SD卡中的文件。我怎么能做到这一点? – AndroidOptimist 2013-11-15 06:01:01

+0

@Alliswell请点击此链接,希望你能得到你的解决方案http://stackoverflow.com/questions/9962896/store-data-text-to-sd-card-in-android – 2013-11-15 19:34:25