{"id":1869,"date":"2013-06-13T05:29:50","date_gmt":"2013-06-13T05:29:50","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=1869"},"modified":"2013-06-13T06:30:33","modified_gmt":"2013-06-13T06:30:33","slug":"copy-supporting-files-apk-installation","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2013\/06\/13\/copy-supporting-files-apk-installation\/","title":{"rendered":"How To Copy Supporting Files During apk Installation"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushJava.js\"><\/script>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2013\/06\/boxes.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-1906\" alt=\"boxes\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2013\/06\/boxes-238x300.jpg\" width=\"238\" height=\"300\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2013\/06\/boxes-238x300.jpg 238w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2013\/06\/boxes.jpg 377w\" sizes=\"auto, (max-width: 238px) 100vw, 238px\" \/><\/a>For 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:<\/p>\n<p><strong>STEP 1<\/strong><\/p>\n<p>Put all supporting files in the <em>assets<\/em> folder. If you dont have an <em>assets<\/em> 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 <em>data.xml<\/em> in a subfolder called <em>support .\u00a0<\/em>So you will first create a folder called <em>support <\/em>under assets and then copy the file <em>data.xml <\/em>into that.<\/p>\n<p><strong>STEP 2<\/strong><\/p>\n<p>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<\/p>\n<p><pre class=\"brush: java\">\t@Override\r\n\tprotected void onCreate(Bundle savedInstanceState) {\r\n\t\tsuper.onCreate(savedInstanceState);\r\n\t\tsetContentView(R.layout.activity_main);\r\n\t\t\r\n\t\tString mDirPath = getDataPath();\r\n\t\tString mPath = mDirPath +  File.separator + &quot;data.xml&quot;;\r\n\t\t\r\n\t\t\/\/ only copy if file is not already present\r\n               if (!(new File(mPath).exists()) {\r\n            try {\r\n                AssetManager assetManager = getAssets();\r\n                InputStream in = assetManager.open(&quot;support\/data.xml&quot;);\r\n                \r\n                \r\n                File dir = new File(mDirPath);\r\n                if (!dir.exists())\r\n                \tdir.mkdirs();\r\n                File datadir = new File(mDirPath, &quot;support&quot;);\r\n                if (!datadir.exists())\r\n                \tdir.mkdirs();\r\n             \r\n                File dataFile = new File(datadir, &quot;data.xml&quot;);\r\n                DataOutputStream outw = new DataOutputStream(new FileOutputStream(\r\n    \t\t\t\t\tdataFile.getAbsolutePath()));\r\n                \r\n                byte[] buf = new byte[80000];\r\n                int len;\r\n                while ((len = in.read(buf)) &gt; 0) {\r\n                    outw.write(buf, 0, len);\r\n                }\r\n                in.close();\r\n                outw.close();\r\n           \r\n                \r\n                            \r\n                \r\n            } catch (IOException e) {\r\n                android.util.Log.e(&quot;Error&quot;, e.toString());\r\n            }\r\n        } \t\t\r\n\t\t\r\n\t\r\n\t\t\t\t\r\n\t\t\r\n\t}<\/pre><br \/>\nThe code below shows the method getDataPath() which actually can be in a separate class of its own , since its a kind of utility function.<\/p>\n<p><pre class=\"brush: java\">  public static String getDataPath() {\r\n        String returnedPath = &quot;&quot;;\r\n        final String mDataDirName = &quot;support&quot;;\r\n        if(isSDCardMounted()) {\r\n            final String mSDCardPath = Environment.getExternalStorageDirectory() + File.separator + mDirName;\r\n            if(isDirectoryExists(mSDCardPath)) {\r\n                final String mSDCardDataPath = Environment.getExternalStorageDirectory() + \r\n                        File.separator + mDataDirName;\r\n                isDirectoryExists(mSDCardDataPath);\r\n                return mSDCardPath;\r\n            }\r\n        }\r\n        else\r\n        \treturnedPath = mDataDirName;\r\n        return returnedPath;\r\n    }<\/pre><\/p>\n<p><strong>NOTES<\/strong><\/p>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>For 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 <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2013\/06\/13\/copy-supporting-files-apk-installation\/\" title=\"How To Copy Supporting Files During apk Installation\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":1906,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[295],"tags":[],"class_list":["post-1869","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android-dev"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/1869","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=1869"}],"version-history":[{"count":16,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/1869\/revisions"}],"predecessor-version":[{"id":1895,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/1869\/revisions\/1895"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/1906"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=1869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=1869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=1869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}