在仿真数据库文件所在的/data/data/com.example.app_name/databases/data.db内。 但是,在实际的设备不存在此目录。 由于该应用程序崩溃。 应用程序在模拟器运行平稳。
在真实设备的Android / data文件夹存在,但它不包含我命名的文件夹的应用程序。
此外,当我经由吐司显示的目录的应用程序通过this.getDatabasePath()在设备内它显示//data/data/com.example.My_App但只要数据库活动启动应用程序崩溃。 而com.example.My_App文件夹不存在于Android的/数据。
我曾尝试创建内部的Android /数据数据文件夹,并提出了所有的数据库文件中,但仍然没有奏效。 也试过在那里的Android文件夹存在,但什么都没有发生手机内存创建它。 请帮助我。 答案将不胜感激。
为了克服这个问题,您可以创建在移动SD卡数据库。 它顺利工作,也是由用户访问。 如果安全是关心则使用RES /原始文件夹,以便用户没有访问它。 对于SD卡上创建数据库,你可以用下面的代码(这是示例代码)...
public static void saveExpanseForm(Context con,NewExpenseForm newexpanse) throws Exception {
SQLiteDatabase sampleDB = null;
try {
File sdcard;
if (android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED)){
sdcard = new File(Environment.getExternalStorageDirectory(), "ROIApp");
sdcard.mkdirs();
} else {
/* save the folder in internal memory of phone */
sdcard = new File(Environment.getRootDirectory(),"ROIApp");
sdcard.mkdirs();
}
String dbfile = sdcard.getAbsolutePath() + File.separator+ "RoiAppDB" + File.separator + NETNOTENABLE_DB_NAME;
sampleDB = con.openOrCreateDatabase(dbfile, MODE_PRIVATE,null);
sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " + Expense_TABLE_NAME +
" (id INTEGER PRIMARY KEY AUTOINCREMENT, Currentdate varchar, dateFormat varchar, Name varchar, Category varchar,"+
" Location varchar,Cost varchar,Sex INTEGER, Tags varchar, Notes varchar, In1 varchar, In2 varchar, " +
"Remindme INTEGER, ImagePath varchar);");
sampleDB.execSQL("INSERT INTO " + Expense_TABLE_NAME +
"(Currentdate, dateFormat, Name, Category, Location,Cost, Sex, Tags, Notes, In1, In2, Remindme, ImagePath ) Values ('"
+newexpanse.getDates()+"','"+newexpanse.getDateFormat()+"','"+newexpanse.getName()+"','"+
newexpanse.getCategory()+"','"+newexpanse.getLocation()+"','"+newexpanse.getCost()+"','"+newexpanse.getSex()+"','"+newexpanse.getTag()+"','"
+newexpanse.getNotes()+"','"+newexpanse.getInstr1()+"','"+newexpanse.getInstr2()+"','"
+newexpanse.getRemindme()+"','"+newexpanse.getImageName()+"');");
Log.e("ExpanseTable", " ExpanseInsert "+"INSERT INTO " + Expense_TABLE_NAME +
"(Currentdate, dateFormat, Name, Category, Location,Cost, Sex, Tags, Notes, In1, In2, Remindme,ImagePath ) Values ('"
+newexpanse.getDates()+"','"+newexpanse.getDateFormat()+"','"+newexpanse.getName()+"','"+
newexpanse.getCategory()+"','"+newexpanse.getLocation()+"','"+newexpanse.getCost()+"','"+newexpanse.getSex()+"','"+newexpanse.getTag()+"','"
+newexpanse.getNotes()+"','"+newexpanse.getInstr1()+"','"+newexpanse.getInstr2()+"','"
+newexpanse.getRemindme()+"','"+newexpanse.getImageName()+"');");
insertNewExpanseForm(sampleDB);
} catch (SQLiteException e) {
// TODO: handle exception
e.printStackTrace();
}
finally {
if (sampleDB != null)
sampleDB.close();
}
}
试试这个样本类从资产复制数据库的应用程序包,并从应用程序包提取数据的基础到SD卡路径
DBHelper.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.util.Log;
public class DBHelper extends SQLiteOpenHelper {
private static String DB_PACKAGE_PATH = "/data/data/applicationPackageName/databases/";
private static String DATABASE_NAME = "data.db";
private SQLiteDatabase db;
private final Context context;
/**
*
* @param context
*/
public DBHelper(final Context context) {
super(context, DATABASE_NAME, null, 1);
this.context = context;
DB_PACKAGE_PATH = "/data/data/" + context.getPackageName()
+ "/databases/";
}
public final void createDataBaseFromAppAssetsDir() throws IOException {
final boolean dbExist = isDBExist();
SQLiteDatabase db_Read = null;
if (!dbExist) {
// ****** required****
// as it will create empty database file.
// and assets file will be overwrite on this db file
db_Read = this.getReadableDatabase();
db_Read.close();
try {
copyDbFile();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
public void copyFromDataPackgeToSdCard() throws IOException {
try {
File sdCard = Environment.getExternalStorageDirectory();
File appDataDir = Environment.getDataDirectory();
if (sdCard.canWrite()) {
String currentDBPath = "//data//"
+ this.context.getPackageName() + "//databases//"
+ DATABASE_NAME;
String backupDBPath = DATABASE_NAME;
File currentDatabase = new File(appDataDir, currentDBPath);
File backupDatabase = new File(sdCard, backupDBPath);
if (currentDatabase.exists()) {
FileChannel src = new FileInputStream(currentDatabase)
.getChannel();
FileChannel dst = new FileOutputStream(backupDatabase)
.getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Exception e) {
Log.e("copyFromDataPackgeToSdCard", e.getMessage());
}
}
private boolean isDBExist() {
final File dbFile = new File(DB_PACKAGE_PATH + DATABASE_NAME);
return dbFile.exists();
}
private void copyDbFile() throws IOException {
final InputStream myInput = context.getAssets().open(DATABASE_NAME);
final String outFileName = DB_PACKAGE_PATH + DATABASE_NAME;
final OutputStream myOutput = new FileOutputStream(outFileName);
final byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public final void openDataBase() throws Exception {
final String myPath = DB_PACKAGE_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
@Override
public final synchronized void close() {
if (db != null)
db.close();
super.close();
}
@Override
public void onCreate(final SQLiteDatabase arg0) {
}
@Override
public void onUpgrade(final SQLiteDatabase arg0, final int arg1,
final int arg2) {
}
}
调用Create DB和复制数据库
@Override
public void onClick(View v) {
try {
switch (v.getId()) {
case R.id.btn_create_db_from_assets:
// Create DataBase From Assets Folder to Application database
// package . u can call this on startup.Use this onStart up
clsHelper.createDataBaseFromAppAssetsDir();
break;
case R.id.btn_copy_to_sdcard:
// Copy database from applicaiton data packages to sd card. to
// avid root access and all for device
clsHelper.copyFromDataPackgeToSdCard();
break;
default:
break;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
在真正的Android设备,你将不能访问数据文件夹。 你需要一个根深蒂固的设备。
我建议你压缩您的数据库文件,并把它放在你的应用程序的RES / raw目录。 而这样做;
// Call this whenever the application starts, or at another suitable place, for example in a
// Content providers create method.
public void createDataBaseIfDoesNotExist() throws IOException {
boolean dbExist = doesDatabaseExist();
if (!dbExist) {
try {
copyDataBase();
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
throw new Error("Error copying database ");
}
}
}
// Extracts the Compressed database file to the database directory of your application
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getResources().openRawResource(R.raw.abbrev);
ZipInputStream zis = new ZipInputStream(myInput);
zis.getNextEntry();
File outFile = myContext.getDatabasePath(DATABASE_NAME);
OutputStream myOutput = new FileOutputStream(outFile);
byte[] buffer = new byte[1024];
int length;
while ((length = zis.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
// Checks if database exists
private boolean doesDatabaseExist() {
SQLiteDatabase checkDB = null;
try {
String myPath =myContext.getDatabasePath(DATABASE_NAME).toString();
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
你必须根除您的手机,以访问应用程序数据库文件,甚至那么你将不得不改变对数据/文件夹的访问权限。 如果你要打开和读取SD卡或资产的一些数据库文件的文件夹,你可以做,没有生根。