How to export files from Android internal storage (without root)

Source: Internet
Author: User
Tags sqlite database

This time the company project, involving data caching, due to the need to buffer too much data, too large, through the network request, and then cached to the local SQLite database, too much time, consumption of traffic. So prepare to save a standard version of the SQLite database (including data), packaged into the apk file, the next thing to do is to update the data, so that the request and operation of the data is very small.

So the question is, how to export the standard version of the SQLite database file (db format) from the internal storage space, and then put it in the Project assets folder?

Want to copy things from the internal storage space, first to root, the phone to Root,app also to get root permissions. This blog does not tell how to copy content through the root, because this method is really stupid, root phone will bring irreversible changes to the phone, if the phone is very expensive, try not to root, even if the phone root, the app to get root privileges, very troublesome, and domestic handset manufacturers rom different, Many mobile phones cannot view and assign files within the internal storage space even if they follow the step-by-step root.

So how does it come true? A simple to ridiculous approach (get this is the security of the Android System vulnerability), in the code, we can access, read things inside the storage space, can also read and write the SD card and other external storage space (to add the appropriate permissions), then we can through the method of Io, It is ok to copy the files from the internal storage space to the external storage space and then copy what we need from the external storage space .

Implementation method:

1. Add System privileges:

Add read/write SD card and other operation permissions in Androidmanifest.xml:

    <!-- Write extended storage, write data to the expansion card  -    <  android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

2. Copy to SD card and external storage space via IO from internal storage:

At this point, the received JSON data has been written to the SQLite database through a network request. Direct copy is good:

/*** Copy the database of internal storage to external * *@throwsFileNotFoundException*/     Public voidCopydbfile ()throwsfilenotfoundexception {File Todir=NewFile (FIELD.DB_PATH_SD);//External Storage Folder        if(!todir.exists ())        {Todir.mkdirs (); } File Todb=NewFile (FIELD.DB_PATH_SD + App.BaseDB.dbName);//External Storage DatabaseFile Fromdir =NewFile (Field.db_path + App.BaseDB.dbName);//Internal Storage DatabaseInputStream is;        OutputStream OS; is=NewFileInputStream (Fromdir); OS=NewFileOutputStream (TODB); byte[] buffer =New byte[1024]; intlength; Try {            /*** Copy Process*/             while(length = is.read (buffer, 0, buffer.length)) > 0) {os.write (buffer,0, length);            } os.flush ();            Os.close ();        Is.close (); } Catch(IOException e) {e.printstacktrace (); }    }

Related constants:

/*** Internal Database path*/     Public Static FinalString Db_path = file.separator + "Data" + environment.getdatadirectory (). GetAbsolutePath () +File.separator+ myapplication.getinstance (). Getpackagename () + file.separator + "databases" +File.separator; /*** External database path*/     Public Static FinalString DB_PATH_SD = Environment.getexternalstoragedirectory (). GetAbsolutePath () + File.separator + "TDR" +File.separator; Public Static classbasedb{ Public Static Final intVersion=1;  Public Static FinalString dbname= "Base.db"; }

3, where needed, call Copydbfile ()

                       The try {                //Copy tool class gets the Basedbhelper.getinstance (). Copydbfile () by a single case (), a            catch (FileNotFoundException e) {                LOG.E (TAG, "Filenotfount");                LOG.E (TAG, E.getmessage ());                LOG.E (TAG, e.tostring ());                E.printstacktrace ();            }        

After the execution, you can see the required DB file directly in the phone folder:

In this way, you can copy the files in the internal storage space on a non-root basis. Have to say, this may be an unintentional security vulnerability of Android! Because this is really stupid, it is like: a family has a safe, inside the property of the house, thief if want to take away from the safe property is not possible, but if the thief first take the money from the safe to the living room, and then take away from the living room, it is entirely possible, so take away, not only no constraints, The home owner will also take the initiative to tell you the safe password, the home door for you open ...

4. PostScript

When I introduced the company project, I mentioned that I would copy the db file to the Assets folder of the project by offline and then manipulate the database in the Assets folder when I needed to manipulate the database. But there's a big performance problem with this:

SQLite database Read and write, can only read and write/data/... Database files in this way, each time you read or modify the database in the assets, you must first copy the assets database to the data corresponding folder, and then read, so that the efficiency is very low, and after the database has been modified, There is no way to synchronize the changes to the source database (that is, the database is automatically restored). So what is the input to solve this problem? There will be time, then a blog, said some of the problem.

How to export files from Android internal storage (without root)

Related Article

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.