4

I am very new to android development and am looking for a way to modify existing source-code in eclipse so that when the apk is installed, an xml file is copied from inside the apk to a specific folder on the external storage.

Is there a way to do this?

m13124
  • 51
  • 1
  • 2
  • 3
  • to add a little detail as to what I am trying to do, I have code for an app that downloads files from a webserver and then does stuff with them. This process of getting the files is currently handled in a gui in the app. Once the file is downloaded to the sd card, the program then does stuff with it. What I want to do is to have the app start off with the correct file and never expose the user to the selection/download part of the GUI. I'm looking for the simplest way to do this as I am not really a coder. – m13124 Mar 20 '11 at 20:57

1 Answers1

4

See the question and answer here...Android: How to create a directory on the SD Card and copy files from /res/raw to it??

EDIT: Thinking about it, I use the /assets folder and not /res/raw. This is roughly what I do...

First get a valid folder on your external storage (normally SD card)...

File myFilesDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.mycompany.myApp/files");

Replace com.mycompany.myApp in the path above with your app's package name.

Then the following will copy all files from the assets folder with filenames beginning with "xyz", e.g., xyz123.txt, xyz456.xml etc.

try {
    AssetManager am = getAssets();
    String[] list = am.list("");
    for (String s:list) {
        if (s.startsWith("xyz")) {
            Log.d(TAG, "Copying asset file " + s);
            InputStream inStream = am.open(s);
            int size = inStream.available();
            byte[] buffer = new byte[size];
            inStream.read(buffer);
            inStream.close();
            FileOutputStream fos = new FileOutputStream(myFilesDir + "/" + s);
            fos.write(buffer);
            fos.close();
        }
    }
}
catch (Exception e) {
    // Better to handle specific exceptions such as IOException etc
    // as this is just a catch-all
}

Note you'll need the following permission in the AndroidManifest.xml file...

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Community
  • 1
  • 1
Squonk
  • 48,735
  • 19
  • 103
  • 135
  • Thanks! That seems like what I'm looking for, but I'm too inept to implement it based on that answer. I'm not really a coder and I'm working on a bit of a kludge here. Is there any way you could show me a code snippet of what this would actually look like when correctly implemented? – m13124 Mar 20 '11 at 20:53
  • @m13124: See from EDIT in my answer above. – Squonk Mar 20 '11 at 21:51
  • Thanks MisterSquonk! I will give this a try :) – m13124 Mar 20 '11 at 21:53
  • And is there a way to do it during APK installation? – Flash Thunder Feb 18 '14 at 06:28
  • 1
    @FlashThunder : No. Unlike something like a Windows app installation, there is no way to customize the Android app procedure. The closest you can get is to do the copying the first time the app is run. – Squonk Feb 18 '14 at 06:57