1

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.

1 Answers1

1

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

Community
  • 1
  • 1
TheLittleNaruto
  • 8,325
  • 4
  • 54
  • 73
  • May be you dont understand my question. I copy xml file to phone, when lunch apps but I want to this function when i install apk to my device, Not any activity call. Is that possible? –  Jan 29 '14 at 07:43
  • Why do you want that ? I mean, after successful installation you can copy the file at first launch of the app if it is needed. – TheLittleNaruto Jan 29 '14 at 08:38