APK installation to copy the program files attached to the mobile phone specified directory

Source: Internet
Author: User

The project has been completed, today to get the. apk file to another non-debug phone to use, found a problem. Because to draw, So the point of the plot of data saved in a. txt text file, the last time directly to the USB to the specified folder, but obviously unscientific, because the user downloaded your. apk file, you tell him, but also to copy the text file to the specified folder, so, I will solve this problem, is to package the text file in the APK file, install the. APK when you create a program folder, and then copy the text files to this directory folders, the user runs the program, you can draw, no longer let him copy the coordinates of the point of the drawing data. In the same vein, I have the data for the design profile and the data for the standard rails to be available initially.

Here is a way to solve the problem.

First of all, you have to name the running necessary files, only the a-z,0-9 36 characters (it is so remind me) composed of the file name, and then in your project to create a new folder raw, generally speaking, this folder is not, because the file under this folder is not compressed, Instead, copy it directly into the APK file and add your file to the raw folder, which is the first step.

Similar to creating a folder, add files on it.

Add the file, then how to copy these files to the specified directory? Here are a few points, in fact, the document operation that piece of knowledge points. First, to determine whether the specified file directory and files exist, do not exist to be created first, and second, to reference the files in raw; Third, copy the raw files to the created file, or raw files to the output stream, and then write the input stream to the file created on the phone.

Here I write a simple function, the function of the first two parameters, one is written to the phone file directory path, one is the newly created file name. The third parameter is most important, that is, the file in raw is indexed by the ID in the R.java under the Gen folder, just as the button in our interface is referenced in the program and is indexed by ID. This third parameter is an int, which is the int type in the Id,r.java of this file, so you can look at it. This function code is as follows:

private void CopyFile (string filedirpath,string filename,int id) {String filePath = Filedirpath + "/" + filename;//file path tr y {file Dir = new File (filedirpath);//directory path if (!dir.exists ()) {///if not present, create path name Dir.mkdirs ();} Directory exists, copy the required documents from the Raw folder in the APK to the directory file File = new file (FilePath); if (!file.exists ()) {//file does not exist InputStream is = GETRESOURC Es (). Openrawresource (ID);//RAW data resource FileOutputStream fs = new FileOutputStream (file); byte[] buffer = new byte[1024]; int count = 0;//Loop writes out while ((count = is.read (buffer)) > 0) {fs.write (buffer, 0, count);} Fs.close ();//Close stream is.close ();}} catch (Exception e) {e.printstacktrace ();}}

We can assign this function in the OnCreate method of the first activity, so that after installing the. apk file, the first time you open the program, you create the specified file, which is copied from raw to the past.

CreateFile ("/sdcard/test/data", "TB60.txt", r.raw.tb60) createFile ("/sdcard/test/guikuo", "Design track", "r.raw.sjgk"); CreateFile ("/sdcard/test/guikuo", "standard track profile. txt", r.raw.bzgk);

The parameters above can be modified by themselves. However, after you install, if you do not run the program, will not find these folders, because this is the first run is to create, the next run, the judge file already exists, do not do any processing.

In fact, this is a very simple detail, is the operation of the file, but sometimes really used to get, of course, not only text files, music files can also be placed in the face, we can try more.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.