【问题标题】:Reading binary file from sdcard using Stream classes in android使用android中的Stream类从sdcard读取二进制文件
【发布时间】:2010-10-05 09:59:36
【问题描述】:

谁能知道如何使用 Streams 读取位于 sdcard 中的二进制文件,例如 InputstreamCountingInputStreamSwappedDataInputStream

我正在使用这三个流来读取当前位于 Resources folder 中的文件,但现在我想将该文件移动到 sdcard 但我无法更改这些流,因为我已经做了很多工作并且我无法回滚我的工作。
我正在这样做,但它给了我FileNotFoundException。我需要你的帮助。

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);

非常感谢您的任何建议。

【问题讨论】:

    标签: java android file filestream


    【解决方案1】:

    这是一个简单的方法,只是将输入流的内容复制到输出流:

        /**
         * 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://code.google.com/p/meneameandroid/source/browse/trunk/src/com/dcg/util/IOUtilities.java

    为了确保目录存在于您要写入/读取数据的位置,我使用了这样的东西:

        /**
         * 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卡读写权限为:

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

    已编辑:链接已更新

    【讨论】:

    • 这意味着路径不正确或 SD 卡上没有您希望的文件。您是否添加了外部存储权限?我添加了一个准备 SD 卡的函数,这样你至少知道该目录存在。
    • 我可以创建目录并将文件放入其中。但主要问题是我无法通过输入流读取这些文件。
    • 尝试创建一个File对象指向你要读取的文件然后检查canRead()方法(developer.android.com/reference/java/io/File.html)
    猜你喜欢
    • 1970-01-01
    • 2011-04-16
    • 2015-07-21
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    • 2018-08-24
    • 2015-07-27
    相关资源
    最近更新 更多