阅读使用Android的Stream类从SD卡的二进制文件 [英] Reading binary file from sdcard using Stream classes in android
问题描述
任何人可以有任何想法如何读取使用流一样的InputStream,CountingInputStream或SwappedDataInputStream?
Can anybody have any idea how to read a binary file which resides in sdcard using Streams, like Inputstream, CountingInputStream or SwappedDataInputStream?
我现在用这三个流读取一个文件,该文件目前在Resouces文件夹中,但现在我想移动在SD卡的文件,但因为我已经做了这么多工作,我不能改变这些流,我无法滚动回我的工作。
我正在做这种方式,但其给我FileNotFoundException异常。我需要你的帮助,请。
I am using these three streams to read a file which is currently in the Resouces folder, but now i want to move that file in sdcard but I cannot change these stream because I have done so much work on it and I cannot roll back my work. i am doing it this way but its giving me FileNotFoundException. i need your help please.
AssetManager assetManager = getResources()getAssets();最终文件
文件夹=新的文件(Environment.getExternalStorageDirectory()+/图);
布尔成功= FALSE;如果(!folder.exists()){
成功= folder.mkdir(); }其他{
Log.i(文件夹中已经存在,文件夹已经存在); }
AssetManager assetManager = getResources().getAssets(); final File folder = new File(Environment.getExternalStorageDirectory() + "/map"); boolean success = false; if(!folder.exists()) { success = folder.mkdir(); } else { Log.i("folder already exists", "folder already exists"); }
尝试{=的IStream
assetManager.open(Environment.getExternalStorageDirectory().getAbsolutePath().concat(\"/map/map.amf\"));
}赶上(IOException异常五){// TODO自动生成catch块e.printStackTrace(); }顺=新
CountingInputStream(istream的);输入=新
SwappedDataInputStream(CIS);
try { iStream = assetManager.open(Environment.getExternalStorageDirectory().getAbsolutePath().concat("/map/map.amf")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } cis = new CountingInputStream(iStream); input = new SwappedDataInputStream(cis);
非常感谢任何建议。
推荐答案
这是一个简单的方法,它只是复制输入流的输出流的内容:
This is a simple method that just copies the content of an input stream to an output stream:
/**
* Copy the content of the input stream into the output stream, using a
* temporary byte array buffer whose size is defined by
* {@link #IO_BUFFER_SIZE}.
*
* @param in
* The input stream to copy from.
* @param out
* The output stream to copy to.
*
* @throws java.io.IOException
* If any error occurs during the copy.
*/
public static void copy(InputStream in, OutputStream out)
throws IOException {
byte[] b = new byte[IO_BUFFER_SIZE];
int read;
while ((read = in.read(b)) != -1) {
out.write(b, 0, read);
}
}
它采取从一个应用程序,我前一阵子做:的http://$c$c.google.com/p/meneameandroid/source/browse/trunk/src/com/dcg/util/IOUtilities.java
和以确保目录存在要读/写数据我用的是这样的:
And to make sure the dir exists where you want to write/read your data I used something like this:
/**
* Prepares the SDCard with all we need
*/
private void prepareSDCard() {
// Create app dir in SDCard if possible
File path = new File("/sdcard/MyAppDirectory/");
if(! path.isDirectory()) {
if ( path.mkdirs() )
{
Log.d(TAG,"Directory created: /sdcard/MyAppDirectory");
}
else
{
Log.w(TAG,"Failed to create directory: /sdcard/MyAppDirectory");
}
}
}
的写权限/从SD卡读的是:
The permission to write/read from the SD card is:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
的编辑:Linkes更新的
这篇关于阅读使用Android的Stream类从SD卡的二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!