从资产中获取文件,读取它并将内容复制到另一个文件中

时间:2016-11-18 10:52:12

标签: android android-assets

我在assets文件夹中有一个文件。我想打开文件,通过InputStream读取byte [],然后使用FileOutputStream将byte []写入另一个文件。

我的代码:

assetManager = MainActivity.this.getAssets();
                assetStream = assetManager.open("qamaster2.pfx");
                File file = StringGenerator.createFileFromInputStream(assetStream, "qamaster2.pfx");

和方法createFileFromInputStream:

public static File createFileFromInputStream(InputStream inputStream, String fileName) {

        try{
            File file = new File(fileName);
            Log.e("File", file.getName());
            if (file.exists()){
                int size = inputStream.available();
                Log.e("File size", String.valueOf(size));
                byte[] buffer = new byte[size];
                inputStream.read(buffer);
                inputStream.close();
            }
            return file;
        }catch (IOException e) {
            Log.e("Exception", "Something happened here");
            e.printStackTrace();
        }

        return null;
    }

但仍然没有任何反应。有什么建议吗?

2 个答案:

答案 0 :(得分:2)

从资产中读取文件

  try {
            InputStream is = context.getAssets().open("file name");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

然后创建新文件并将此InputStream写入该文件。

在清单文件中提供读写权限。

将输入流写入文件,如下所示------

 try {
                InputStream inputStream = null;
                OutputStream output = null;
                try {
                    inputStream = getContentResolver().openInputStream(data.getData());
                    File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES),
                            "picture_G.jpg");
                    output = new FileOutputStream(file);

                    byte[] buffer = new byte[4 * 1024]; // or other buffer size
                    int read;

                    while ((read = inputStream.read(buffer)) != -1) {
                        output.write(buffer, 0, read);
                    }
                    output.flush();


                } finally {
                    inputStream.close();
                    output.close();
                }
            } catch (Exception e) {
                e.printStackTrace(); // handle exception, define IOException and others
            }

答案 1 :(得分:1)

在这种情况下,您还可以按如下方式在本地应用程序文件空间中创建和写入文件(改编自上一个答案):

private File createFileFromInputStream(InputStream inputStream) {
    try{
        File f = new File(getFilesDir(), "temp1.txt");
        OutputStream outputStream = new FileOutputStream(f);
        byte buffer[] = new byte[1024];
        int length = 0;

        while((length=inputStream.read(buffer)) > 0) {
            outputStream.write(buffer,0,length);
        }

        outputStream.close();
        inputStream.close();

        return f;
    } 
    catch (IOException e) { e.printStackTrace(); }
    return null;
}