Java 将文件写入sdcard
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2455102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing a file to sdcard
提问by Sumit M Asok
I'm trying to write a file from an Http post reply to a file on the sdcard. Everything works fine until the byte array of data is retrieved.
我正在尝试将 Http 帖子回复中的文件写入sdcard上的文件。一切正常,直到检索到数据的字节数组。
I've tried setting WRITE_EXTERNAL_STORAGE
permission in the manifest
and tried many different combinations of tutorials I found on the net.
我试过WRITE_EXTERNAL_STORAGE
在清单中设置权限,并尝试了我在网上找到的许多不同的教程组合。
All I could find was using the openFileOutput("",MODE_WORLD_READABLE)
method, of the activity but how my app writes file is by using a thread. Specifically, a thread is invoked from another thread when a file has to be written,
so giving an activity object didn't work even though I tried it.
我所能找到的只是使用活动的openFileOutput("",MODE_WORLD_READABLE)
方法,但我的应用程序如何使用线程写入文件。具体来说,当必须写入文件时,会从另一个线程调用一个线程,因此即使我尝试过,提供一个活动对象也不起作用。
The app has come a long way and I cannot change how the app is currently written.
该应用程序已经取得了长足的进步,我无法更改该应用程序当前的编写方式。
Please, someone help me?
请问有人帮帮我吗?
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
holds the whole files path. seReply
is a string reply from HttpPost
response. The second set of code is looped with reference to x
. The file is created but remains 0 bytes.
bgdmanip.savLocation
保存整个文件路径。seReply
是来自HttpPost
响应的字符串回复。第二组代码参考 循环x
。文件已创建,但仍为 0 字节。
采纳答案by Erich Douglass
The openFileOutput()
method writes data to your application's private data area (not the SD card), so that's probably not what you want. You should be able to call Environment.getExternalStorageDirectory()
to get the root path to the SD card and use that to create a FileOutputStream
. From there, just use the standard java.io routines.
该openFileOutput()
方法将数据写入应用程序的私有数据区(不是 SD 卡),因此这可能不是您想要的。您应该能够调用Environment.getExternalStorageDirectory()
以获取 SD 卡的根路径并使用它来创建FileOutputStream
. 从那里,只需使用标准的 java.io 例程。
回答by gtangil
Here is a sample:
这是一个示例:
// 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);
}
回答by Pir Fahim Shah
//------------------------------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();
}
}
});
ALONG WITH THIS ALSO WRITE THIS PERMISSION IN Android.Manifest
与此同时在 Android.Manifest 中写入此权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />