How To Copy Supporting Files During apk Installation

boxesFor certain apps which require some supporting files to be part of the installation eg.data files or images, there needs to be a way of transferring the files from the apk installer to the device. The transfer does not happen automatically. We look at the steps to do this, below:

STEP 1

Put all supporting files in the assets folder. If you dont have an assets folder already created in the root folder of your app, then create one and put all the required files into it. So lets assume your app needs a file called data.xml in a subfolder called support . So you will first create a folder called support under assets and then copy the file data.xml into that.

STEP 2

You will write code in the onCreate method of your app to transfer the data to the device. This means that every time the app is run, it will execute this code. Since we dont want to transfer files every time the app is run, we put a check first to see if the files have already been transferred. If not then the transfer is done . The code is given below

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		String mDirPath = getDataPath();
		String mPath = mDirPath +  File.separator + "data.xml";
		
		// only copy if file is not already present
               if (!(new File(mPath).exists()) {
            try {
                AssetManager assetManager = getAssets();
                InputStream in = assetManager.open("support/data.xml");
                
                
                File dir = new File(mDirPath);
                if (!dir.exists())
                	dir.mkdirs();
                File datadir = new File(mDirPath, "support");
                if (!datadir.exists())
                	dir.mkdirs();
             
                File dataFile = new File(datadir, "data.xml");
                DataOutputStream outw = new DataOutputStream(new FileOutputStream(
    					dataFile.getAbsolutePath()));
                
                byte[] buf = new byte[80000];
                int len;
                while ((len = in.read(buf)) > 0) {
                    outw.write(buf, 0, len);
                }
                in.close();
                outw.close();
           
                
                            
                
            } catch (IOException e) {
                android.util.Log.e("Error", e.toString());
            }
        } 		
		
	
				
		
	}

The code below shows the method getDataPath() which actually can be in a separate class of its own , since its a kind of utility function.

  public static String getDataPath() {
        String returnedPath = "";
        final String mDataDirName = "support";
        if(isSDCardMounted()) {
            final String mSDCardPath = Environment.getExternalStorageDirectory() + File.separator + mDirName;
            if(isDirectoryExists(mSDCardPath)) {
                final String mSDCardDataPath = Environment.getExternalStorageDirectory() + 
                        File.separator + mDataDirName;
                isDirectoryExists(mSDCardDataPath);
                return mSDCardPath;
            }
        }
        else
        	returnedPath = mDataDirName;
        return returnedPath;
    }

NOTES

The usage of DataOutputStream is preferred because it can handle binary data as well like image files. The method getDataPath() checks if the SD card is mounted in which case it returns the path to that, otherwise it returns the path to the apps internal storage space on the device.

Be the first to comment

Leave a Reply

Your email address will not be published.


*