Android 資料庫打包隨APK發布

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   os   io   java   ar   

有些時候我們的軟體用到SQLite資料庫,這個時候怎麼把一個做好的資料庫打包進我們的APK呢,其實很簡單,就是把我們的資料庫檔案放到我們的手機裡,所以不必局限在哪個地方寫這個代碼,在第一次建立資料庫的時候可以,我覺得在軟體起動頁裡效果更好一點,首先我們應該把事先寫好的資料庫檔案比如test.db放到res檔案夾裡的raw檔案夾裡,也可以放到assets裡,因為這兩個檔案夾不會在產生APK的時候不會被壓縮 

import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream; import com.android.qufu.dinner.MealActivityGroup;import com.android.qufu.dinner.R; import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.content.pm.ActivityInfo;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteException;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.Window;import android.view.WindowManager;import android.widget.ImageView;import android.widget.TextView; public class Loggin extends Activity {    public static String dbName="dinner.db";//資料庫的名字    private static String DATABASE_PATH="/data/data/com.android.qufu.dinner/databases/";//資料庫在手機裡的路徑     int alpha = 255;      int b = 0;           public void onCreate(Bundle savedInstanceState) {             super.onCreate(savedInstanceState);             setContentView(R.layout.login);             //判斷資料庫是否存在            boolean dbExist = checkDataBase();            if(dbExist){                             }else{//不存在就把raw裡的資料庫寫入手機                try{                    copyDataBase();                }catch(IOException e){                    throw new Error("Error copying database");                }            }                         new Thread(new Runnable() {                 public void run() {                     initApp(); //初始化程式                                           while (b < 2) {                         try {                             if (b == 0) {                                 Thread.sleep(20);                                 b = 1;                             } else {                                 Thread.sleep(50);                             }                             updateApp();                         } catch (InterruptedException e) {                             e.printStackTrace();                         }                     }                 }             }).start();                      }          public void updateApp() {             alpha -= 5;             if (alpha <= 0) {                 b = 2;                 if(true){                try{                   Intent in = new Intent(Loggin.this,MealActivityGroup.class);                    Loggin.this.startActivity(in);                    Loggin.this.finish();                }catch(Exception e){                                             }                }                            }                     }         /**         * 判斷資料庫是否存在         * @return false or true         */        public boolean checkDataBase(){            SQLiteDatabase checkDB = null;            try{                String databaseFilename = DATABASE_PATH+dbName;                checkDB =SQLiteDatabase.openDatabase(databaseFilename, null,                        SQLiteDatabase.OPEN_READONLY);            }catch(SQLiteException e){                             }            if(checkDB!=null){                checkDB.close();            }            return checkDB !=null?true:false;        }        /**         * 複製資料庫到手機指定檔案夾下         * @throws IOException         */        public void copyDataBase() throws IOException{            String databaseFilenames =DATABASE_PATH+dbName;            File dir = new File(DATABASE_PATH);            if(!dir.exists())//判斷檔案夾是否存在,不存在就建立一個                dir.mkdir();            FileOutputStream os = null;            try{                os = new FileOutputStream(databaseFilenames);//得到資料庫檔案的寫入流            }catch(FileNotFoundException e){                e.printStackTrace();            }            InputStream is = Loggin.this.getResources().openRawResource(R.raw.test);//得到資料庫檔案的資料流            byte[] buffer = new byte[8192];            int count = 0;            try{                                 while((count=is.read(buffer))>0){                    os.write(buffer, 0, count);                    os.flush();                }            }catch(IOException e){                             }            try{                is.close();                os.close();            }catch(IOException e){                e.printStackTrace();            }        }        /**         * 初始化,這裡是起始頁的沒有用         */        public void initApp(){                  }     }

參考:http://www.oschina.net/code/snippet_203635_9207

Android 資料庫打包隨APK發布

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.