简体   繁体   English

在安装APK文件时将XML文件从资产复制到外部存储

[英]Copy XML file from assets to external storage while installing apk file

I am developing an android app which needs to copy the existing XML file from assets folder to external storagewhile installing the apk file in device . 我正在开发一个Android应用程序,需要在设备中安装apk文件时将现有XML文件从资产文件夹复制到外部存储中 Is there any inbuilt function for it or other technique to call my method while installing apk file. 在安装apk文件时是否有任何内置函数或其他技术可以调用我的方法。

Thanks in Advance. 提前致谢。

You can copy your xml file from assets folder by given code : 您可以通过给定的代码从资产文件夹复制xml文件:

File toPath = Environment.getExternalStoragePublicDirectory(mAppDirectory);
    if (!toPath.exists()) {
        toPath.mkdir();
    }

    try {
        InputStream inStream = getAssets().open("file.xml");
        BufferedReader br = new BufferedReader(new InputStreamReader(inStream));
        File toFile = new File(toPath, "file.xml");
        copyAssetFile(br, toFile);
    } catch (IOException e) {
    }

private void copyAssetFile(BufferedReader br, File toFile) throws IOException {
    BufferedWriter bw = null;
    try {
        bw = new BufferedWriter(new FileWriter(toFile));

        int in;
        while ((in = br.read()) != -1) {
            bw.write(in);
        }
    } finally {
        if (bw != null) {
            bw.close();
        }
        br.close();
    }
}

Reference : LINK 参考: LINK

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

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