【问题标题】:Get a file from assets, read it and copy the contents in another file从资产中获取文件,读取它并将内容复制到另一个文件中
【发布时间】:2023-03-28 12:52:01
【问题描述】:

我在资产文件夹中有一个文件。我想打开文件,通过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;
    }

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

【问题讨论】:

  • 但是在你的代码中你从来没有将你得到的输入流“写”到一个新文件中,你希望如何工作?

标签: android android-assets


【解决方案1】:

像这样从资产中读取文件

  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
            }

【讨论】:

  • 我也想从中创建一个文件。文件的创建给我带来了麻烦
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    相关资源
    最近更新 更多