将文件写入SD卡

时间:2010-03-16 14:25:57

标签: java android file file-io

我正在尝试将Http帖子回复中的文件写入 sdcard 上的文件。一切正常,直到检索到数据的字节数组。

我已尝试在清单中设置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字节。

3 个答案:

答案 0 :(得分: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 :(得分:22)

openFileOutput()方法将数据写入应用程序的私有数据区(而不是SD卡),因此可能不是您想要的。您应该可以致电Environment.getExternalStorageDirectory()以获取SD卡的根路径并使用它创建FileOutputStream。从那里,只需使用标准的java.io例程。

答案 2 :(得分: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);
}
相关问题