0
点赞
收藏
分享

微信扫一扫

android APK 带数据库


 首先android导出APK是不自带数据库的,就是说,如果你的应用程序要用到数据库的话,那么就要格外写代码进行操作了。

 

 

直接上代码

首先在res目录下创建一个raw的文件,里面存放一个数据库名字叫dict_db4.db

然后写代码了

 

MainActivity.java

 

package com.hzilearning.dict.activity1;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	private Button btn ;
	private TextView tv ;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
      
       
       this.btn = (Button) this.findViewById(R.id.button1) ;
       this.tv = (TextView) this.findViewById(R.id.textView1) ;
	   this.btn.setOnClickListener(new OnClickListener() {

		@Override
		public void onClick(View v) {
			    
			 System.out.println("1") ;
			     
			     File dir = null ;
			     File dirDatabase = null ;
		         Context cont = getApplicationContext();
		         File file =  cont.getFilesDir() ;
		         File parentFile = file.getParentFile() ;
		         if (parentFile.exists()) {
		        	 System.out.println("2") ;
		        	  String path = parentFile.getPath()+ "/databases" ;
		        	  dir = new File(path);
		             if (!dir.exists())
		            	 System.out.println("3") ;
						try {
							dir.mkdir() ;
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						 String path2 = parentFile.getPath()+ "/databases/dict_db4.db" ;
						 dirDatabase = new File(path2);
			             if (!dirDatabase.exists())
			            	 System.out.println("3") ;
							try {
								dirDatabase.createNewFile() ;
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						
		         }
		         

		        InputStream is = getResources().openRawResource(R.raw.dict_db4);  
			   
	            FileOutputStream fos = null;
				try {
					fos = new FileOutputStream(dirDatabase);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  
	            byte[] buffer = new byte[8192];  
	            int count = 0;  
	            // 开始复制dictionary.db文件  
	            try {
					while ((count = is.read(buffer)) > 0)  
					{  
					    fos.write(buffer, 0, count);  
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  
	  
	            try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  
	            try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	}

	}) ;
       
       
        
    }
}

main.xml

 

<?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:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

 

你运行程序会发现在raw文件夹下的数据库复制到了我们的应用数据库文件下了

举报

相关推荐

0 条评论