Skip to main content
Copy database from assets folder.

Keep database in your assets folder with predefine tables and with the help below class you can copy database into your app database folder from assets folder.

databaseHelper.java
public class databasehelper extends SQLiteOpenHelper {
    private static String DATABASE_NAME = "databasename.sqlite";
    public static SQLiteDatabase myDataBase;
    private Context myContext;
    private String path = "/data/data/packagename/databases/";
    private static CategoryWiseQandA item;

    public databasehelper(Context context) {
  super(context, DATABASE_NAME, null, 1);
        this.myContext = context;
        try {
            createDataBase();
            openDataBase();
        } catch (IOException e) {
             e.printStackTrace();
        }

    }
    // ---Create the database---
    public void createDataBase() throws IOException {

        // ---Check whether database is already created or not---
        boolean dbExist = checkDataBase();

        if (!dbExist) {
            this.getReadableDatabase();
            try {
                // ---If not created then copy the database---
                copyDataBase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
        }

    }
    // --- Check whether database already created or not---
    private boolean checkDataBase() {
        try {
            String myPath = path + DATABASE_NAME;
            File f = new File(myPath);
            if (f.exists())
                return true;
            else
                return false;
        } catch (SQLiteException e) {
            e.printStackTrace();
            return false;
        }

    }

    // --- Copy the database to the output stream---
    private void copyDataBase() throws IOException {
        try {
            InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
            String outFileName = path + DATABASE_NAME;
           OutputStream myOutput = new FileOutputStream(outFileName);

            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }

            myOutput.flush();
            myOutput.close();
            myInput.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void openDataBase() throws SQLException {

        // --- Open the database---
        String myPath = path + DATABASE_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
    }
    @Override
    public synchronized void close() {

        if (myDataBase != null)
            myDataBase.close();

        super.close();

    }
}



Comments

Popular posts from this blog

Getview Method Wierd in Listview

Its common issue in android and most of newbee faced. To understand why its happening, you have to read about behavior of Getview method of listivew, its your home work, let continue with tutorial. Below tutorial will explain that how to maintain the position of button  which is in row file using settag and gettag. main.xml ( its contain listview which will hold row data) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:descendantFocusability="blocksDescendants">  <ListView         android:id="@+id/listView"         android:layout_width="fill_parent"         android:layout_height="fill_parent"/> </LinearLayout> ...

Amazon S3 Configuration

Gradles //amazon s3 compile 'com.amazonaws:aws-android-sdk-core:2.2.+' compile 'com.amazonaws:aws-android-sdk-s3:2.2.+' Declare in manefiest file <!--AMAZON S3--> < service android :name= "com.amazonaws.mobileconnectors.s3.transferutility.TransferService" android :enabled= "true" /> AmazonUtils.java import android.content.Context; import android.net.Uri; import com.amazonaws.auth.CognitoCachingCredentialsProvider; import com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver; import com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3Client; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Map; import java.util.UUID; /* * Handles basic helper functions used throughout the app. */ public class AmazonUtil { // We only n...
Update Progress in notification: public class NetworkService extends IntentService {     public static final String FILE_PATH = "file_path";     public static final String _FILE_PATH = "_path";     public static final String PERCENT_EXTRA = "percent";     public static final String FILE_NAME_EXTRA = "FileName";     public static final String STATE_EXTRA = "state";     public static final String IS_ALBUM_UPLAODING = "IsAlbumUplaoding";     public static final String XML = "xml";     public static final String TOKEN = "token";     public static final String MESSAGE = "mesasge";     public static final String POST_DATE = "postdate";     public static final String USERID = "userid";     public static final String GROUPID = "groupid";     public static final String PATH = "path"; ...