将文件写入sdcard [英] Writing a file to sdcard

查看:32
本文介绍了将文件写入sdcard的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

I've tried setting WRITE_EXTERNAL_STORAGE permission in the manifest and tried many different combinations of tutorials I found on the net.

我所能找到的只是使用活动的 openFileOutput("",MODE_WORLD_READABLE) 方法,但我的应用程序如何使用线程写入文件.具体来说,当必须写入文件时,从另一个线程调用一个线程,因此,即使我尝试过,也无法提供活动对象.

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.

应用程序已经取得了长足的进步,我无法改变应用程序当前的编写方式.

The app has come a long way and I cannot change how the app is currently written.

请有人帮帮我吗?

代码:

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 字节.

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.

推荐答案

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

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.

这篇关于将文件写入sdcard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆