简体   繁体   中英

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 . Is there any inbuilt function for it or other technique to call my method while installing apk file.

Thanks in Advance.

You can copy your xml file from assets folder by given code :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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