简体   繁体   English

如何将android应用程序资产文件夹中的文本文件作为文本或pdf文件保存到android手机的本地目录?

[英]How to save a text file present in asset folder of android app to the local directory of android phone as text or pdf file?

I have saved some text files in asset folder of android apk , whenever user opens the app which is having list of filenames ,if he clicks on a filename corresponding file opens from asset folder ,我在 android apk 的资产文件夹中保存了一些文本文件,每当用户打开具有文件名列表的应用程序时,如果他单击文件名,则从资产文件夹中打开相应的文件,

Now I want to let user save that file in text or pdf format to his local directory so that he can transfer it or use it or modify it according to his choice现在我想让用户将该文件以文本或 pdf 格式保存到他的本地目录,以便他可以根据自己的选择传输或使用或修改它

EDIT : I just want to know the api needed ,not the whole code编辑:我只想知道需要的 api,而不是整个代码

Use this function to copy something from assets to storage使用此功能将某些内容从资产复制到存储

 private void copyAssets() {
        AssetManager assetManager = getAssets();
        String[] files = null;
        try {
            files = assetManager.list("");
        } catch (IOException e) {
            Log.e("tag", "Failed to get asset file list.", e);
        }
        if (files != null) for (String filename : files) {
            InputStream in = null;
            OutputStream out = null;
            try {
              in = assetManager.open(filename);
              File outFile = new File(getExternalFilesDir(null), filename);
              out = new FileOutputStream(outFile);
              copyFile(in, out);
            } catch(IOException e) {
                Log.e("tag", "Failed to copy asset file: " + filename, 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);
        }
    }

Reference :Move file using Java参考:使用 Java 移动文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM