[英]Reading binary file from sdcard using Stream classes in android
Can anybody have any idea how to read a binary file which resides in sdcard
using Streams, like Inputstream
, CountingInputStream
or SwappedDataInputStream
? 任何人都可以有任何想法如何阅读驻留在一个二进制文件
sdcard
使用流一样Inputstream
, CountingInputStream
或SwappedDataInputStream
?
I am using these three streams to read a file which is currently in the Resources 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. 我正在使用这三个流来读取当前位于
Resources folder
中的Resources folder
,但是现在我想将该文件移动到sdcard
但是由于我已经做了很多工作并且无法回滚我的工作,因此我无法更改这些流。
I am doing it this way but its giving me FileNotFoundException
. 我这样做,但它给了我
FileNotFoundException
。 I need your help please. 我需要你的帮助。
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");
}
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);
Thanks a lot for any suggestion. 非常感谢您的任何建议。
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);
}
}
It's taken from an app that I made a while ago: http://code.google.com/p/meneameandroid/source/browse/trunk/src/com/dcg/util/IOUtilities.java 它是从我之前制作的一个应用程序中提取的: http : //code.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");
}
}
}
The permission to write/read from the SD card is: 从SD卡写入/读取的权限为:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Edited: Linkes updated 编辑:链接更新
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.