Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Copy files from assets to /data/data folder

I need to use some files in my app. They are kept in asset folder. I saw discussions on SO, where the files are being copied from asset folder, to /data/data/<package_name> on the internal storage, and then being used. I get the code, but what I do not get is, what is the need to copy the assets to internal storage?

like image 765
superuser Avatar asked Apr 07 '14 04:04

superuser


People also ask

How do you copy Assets?

Right-click on the asset in the Navigation Panel and choose Copy, or choose Copy from the More menu. Enter a New Page Name for the copied asset.

How do I add files to Assets Folder?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 – Create a new Assets folder and write a new text file in it.

What is the use of Assets Folder in Android?

Assets provide a way to include arbitrary files like text, xml, fonts, music, and video in your application. If you try to include these files as "resources", Android will process them into its resource system and you will not be able to get the raw data.


1 Answers

public final String path = "/data/data/com.aliserver.shop/databases/";
public final String Name = "store_db";

public void _copydatabase() throws IOException {

        OutputStream myOutput = new FileOutputStream(path + Name);
        byte[] buffer = new byte[1024];
        int length;
        InputStream myInput = MyContext.getAssets().open("store_db");
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        myInput.close();
        myOutput.flush();
        myOutput.close();

    }
like image 72
ali server Avatar answered Sep 19 '22 14:09

ali server