2011-11-29 90 views
4

在將我的應用程序安裝到Android設備時,數據庫文件正在存儲在SD卡根文件夾中。但它應該放在路徑<SD Card_Root>\<Project_Name>\<dbfile>。之後,從設備上卸載應用程序後,包含項目文件夾的數據庫文件也應該被刪除。所以,請幫我解決兩個問題,如下所述:Android - Sqlite數據庫文件在卸載應用程序時未被刪除?

1.安裝apk文件到設備後,數據庫文件應放在路徑<SD Card_Root>\<Project_Name>\<dbfile>。 2.卸載apk文件後,包含根文件夾的數據庫文件應自動刪除。

請幫我提供你的想法/鏈接。

這裏是我的代碼:

import java.io.BufferedInputStream; 
    import java.io.ByteArrayOutputStream; 
    import java.io.File; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 
    import java.io.InputStream; 
    import java.io.OutputStream; 
    import java.util.zip.ZipEntry; 
    import java.util.zip.ZipInputStream; 

    import android.os.Environment; 

    import android.content.Context; 
    import android.database.Cursor; 
    import android.database.SQLException; 
    import android.database.sqlite.SQLiteDatabase; 
    import android.database.sqlite.SQLiteException; 
    import android.database.sqlite.SQLiteOpenHelper; 

    public class DataBaseHelper extends SQLiteOpenHelper{ 
private static String DB_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() ; 
private static String DB_NAME = "Database.db"; 
private SQLiteDatabase myDataBase; 
private final Context myContext; 



public DataBaseHelper(Context context) {   
    super(context, DB_NAME, null, 2); 
    this.myContext = context; 
    } 
public void createDataBase() throws IOException 
{ 
    // boolean dbExist = checkDataBase();   
    DB_PATH = Environment.getExternalStorageDirectory().getAbsolutePath(); 
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) 
    { 
     File db = new File(DB_PATH, DB_NAME); 
     if(!db.exists()) 
     { 
      db.createNewFile(); 
      try { 
        copyFromZipFile(); 
       } catch (IOException e) { 
       throw new Error("Error copying database",e); 
       } 
     } 


     }   
} 

private void copyFromZipFile() throws IOException{ 
    InputStream is = myContext.getResources().openRawResource(R.raw.database); 
     // Path to the just created empty db 
     File outFile = new File(DB_PATH ,DB_NAME); 
      //Open the empty db as the output stream 
     OutputStream myOutput = new FileOutputStream(outFile.getAbsolutePath()); 
     ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is)); 
      try { 
       ZipEntry ze = null; 
       while ((ze = zis.getNextEntry()) != null) { 
        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
        byte[] buffer = new byte[24 * 1024]; 
        int count; 
        while ((count = zis.read(buffer)) != -1) { 
         baos.write(buffer, 0, count); 
         //Log.d("", buffer.toString()); 
         baos.writeTo(myOutput); 
         baos.reset(); 
        } 
        baos.writeTo(myOutput); 


       } 
      } 
      catch (IOException e) {} 
       zis.close(); 
       myOutput.flush(); 
       myOutput.close(); 
       is.close(); 


      /*finally { 
       zis.close(); 
       myOutput.flush(); 
       myOutput.close(); 
       is.close(); 
      } */ 
     } 


private boolean checkDataBase(){   
     SQLiteDatabase checkDB = null; 
     try{ 
      String myPath = DB_PATH +""+ "/" + DB_NAME; 
      checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 
     }catch(SQLiteException e){ 

      //database does't exist yet. 

     }  
     if(checkDB != null){  
      checkDB.close();  
     }  
     return checkDB != null ? true : false; 
    } 
public void openDataBase() throws SQLException{ 

     //Open the database 
     String myPath = DB_PATH + "/" + DB_NAME; 
     myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); 

    } 
@Override 
    public synchronized void close() { 
      if(myDataBase != null) 
       myDataBase.close(); 
      super.close(); 
    } 

@Override 
    public void onCreate(SQLiteDatabase db) {  
    String sql = "create table if not exists info(_id integer primary key autoincrement, Name text not null, firmName text not null);"; 
    db.execSQL(sql); 
    sql = "create table if not exists Client(_id integer primary key autoincrement, Name text not null, clientName text not null);"; 
    db.execSQL(sql); 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS employee"); 
    db.execSQL("DROP TABLE IF EXISTS info"); 
    db.execSQL("DROP TABLE IF EXISTS Client"); 
    onCreate(db); 
    } 
public void onDestroy() 
{ 
    myDataBase.close(); 
    } 
    public Cursor getView(String sql) 
     { 
if(myDataBase != null) { 
    myDataBase.close(); 

    } 
    openDataBase(); 
    Cursor xcur = myDataBase.rawQuery(sql,null); 
     return xcur; 

} 
private void copyDataBase() throws IOException{ 

     //Open your local db as the input stream 
     InputStream myInput = myContext.getAssets().open(DB_NAME); 
     // Path to the just created empty db 
     String outFileName = DB_PATH + DB_NAME; 
     //Open the empty db as the output stream 
     OutputStream myOutput = new FileOutputStream(outFileName); 
     //transfer bytes from the inputfile to the outputfile 
     byte[] buffer = new byte[1024]; 
     int length; 
     while ((length = myInput.read(buffer))>0){ 
      myOutput.write(buffer, 0, length); 
     } 

     //Close the streams 
     myOutput.flush(); 
     myOutput.close(); 
     myInput.close(); 

    } 

}

+0

你好你是否在運行時創建數據庫,或者你提供了創建數據庫的特定路徑。 – Herry

回答

13

如果你想DB文件(或任何其他文件)來安裝應用程序卸載刪除,它應該放在以下路徑:

sd_card/Android/data/your.package.name/

16

您應始終使用Context.getExternalFilesDir(String)來查找存儲應用程序擁有的文件在SD卡上的路徑。請不要爲此類或其他任何標準位置使用任何類型的手工製作路徑;請始終使用平臺在ContextEnvironment中提供的API。

請注意,平臺僅在引入此API的位置開始刪除這些文件(API level 8)。在此之前,您無法將文件存儲在SD卡上,這會導致您的應用程序卸載時被刪除。

相關問題