将资源文件夹中的rar文件复制到外部卡

时间:2016-06-22 11:26:51

标签: android fileoutputstream

我正在尝试将两个文件从assets文件夹复制到外部存储,一个是基于文本的,另一个是rar文件(60 kb)。但是当我在文件管理器中打开rar文件时,它会显示“错误的标题”。我得到的rar文件的大小是16kb。

private void copyAsset() {
        AssetManager assetManager = getAssets();

            InputStream in = null;
            OutputStream out = null;
            try {
                in = assetManager.open(SKETCH_FILE);
                File root = android.os.Environment.getExternalStorageDirectory();
                File dir = new File(root.getAbsolutePath() + "/Ardumation");
                if (!dir.exists()) {
                    //System.out.println("creating directory: " + directoryName);
                    dir.mkdir();
                }
                //File outFile = new File(getExternalFilesDir(null), SKETCH_FILE);
                File sketchFile = new File(dir, SKETCH_FILE);
                File libraryFile = new File(dir, LIBRARY_FILE);

                if (!sketchFile.exists()){
                    out = new FileOutputStream(sketchFile);
                    copyFile(in, out);
                    }
                if (!libraryFile.exists()){
                    out = new FileOutputStream(libraryFile);
                    copyFile(in, out);
                }

            } catch(IOException e) {
                Log.e(TAG, "Failed to copy asset file: ", e);
            }
            finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        // NOOP
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        // NOOP
                    }
                }
            }

    }

    private void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while((read = in.read(buffer)) != -1){
            out.write(buffer, 0, read);
        }
    }

现在出了什么问题......?

0 个答案:

没有答案