How to make apk create a file on an SD card when installing apk?

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

Is there any way to do this?

+4
source share
1 answer

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

EDIT : reflecting on this, I use the / assets folder, not / res / raw. This is roughly what I am doing ...

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

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

Replace com.mycompany.myApp on the path above with your application package name.

Then the following will copy all the files from the folder with the file names starting with "xyz", for example, 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 } 

Please note that you will need the following permission in the AndroidManifest.xml ...

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+3
source

Source: https://habr.com/ru/post/1344493/


All Articles