Reading binary file from sdcard using Stream classes in android

左心房为你撑大大i 提交于 2019-12-25 16:54:26

问题


Can anybody have any idea how to read a binary file which resides in sdcard using Streams, like Inputstream, CountingInputStream or 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.
I am doing it this way but its giving me 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.


回答1:


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

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:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Edited: Linkes updated



来源:https://stackoverflow.com/questions/3862581/reading-binary-file-from-sdcard-using-stream-classes-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!