我尝试了多种方法将我的sqlite db文件复制到我的 /data/data/packagename/databases文件夹,但我仍然被困在FileNotFoundException中,由FileOutputStream对象触发...下面是代码:
public static boolean checkCopyDb(Context c, DbHandler _db) {
try {
String destPath = "/data/data/" + c.getPackageName() + "/databases/db_sociallibraries.db";
File dbFile = new File(destPath);
if(!dbFile.exists()) {
_db.copyDb(c.getAssets().open(DB_NAME), new FileOutputStream(destPath)); // Line 44 - Throws the exception
}
return true;
}
catch (IOException e) {
e.printStackTrace();
return false;
}
}
private void copyDb(InputStream inputStream, OutputStream outputStream) throws IOException {
byte[] buffer = new byte[1024];
int length;
while((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer,0,length);
}
inputStream.close();
outputStream.close();
}
这就是错误:
02-09 01:28:46.384 24222-24222/ /data/dataW/Scom.test.michelemadeddu.sociallibraries︰ystem.errFileNotFoundException:tream.java:117/com.test.michelemadeddu.sociallibraries.:打开失败:ENOENT(没有这样的html" target="_blank">文件或目录)02-09 01:28:46.384 24222-24222/andler.checkW/Sandler.java:44︰libcore.io.IoBridge.open(IoB02-09 01:28:46.384 24222-24222)ridge.java:409/02-09 01:28:46.384 24222-24222W/S02-09 01:28:46.384 24222-24222︰02-09 01:28:46.384 24222-24222FileOutputStream。(FileOutputStream.java:88)com.test.michelemadeddu.sociallibraries/com.test.michelemadeddu.sociallibrariesW/System.err︰java.io.FileOutputStream。(FileOutputStream.java:128)ystem.err/com.test.michelemadeddu.sociallibrariesW/System.err︰java.io.FileOutputStream。(FileOutputScom.test.michelemadeddu.sociallibraries)java.io./ystem.errW/Sjava.io.︰com.test.michelemadeddu.sociallibraries/databases/db_sociallibraries.dbDbHcom.test.michelemadeddu.sociallibrariesCopyDb(DbHystem.err)
我做错什么了吗?谢谢
请尝试此代码...这是我使用的,工作正常。
只需替换DB\u NAME、DATABASE\u NAME和DB\u PATH变量。
package your.packagename;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class database extends SQLiteOpenHelper{
//The Android's default system path of your application database.
private static String DB_PATH = "/data`/data/com.example.applicationame/databases/";`
// Database Name
private static String DB_NAME = "DB.sqlite";
// Logcat tag
private static final String LOG = "database";
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "DB.sqlite";
// Table Names
private static final String TABLE_COUNTIES = "Counties";
private static final String TABLE_DESCRIPTIONS = "Descriptions";
private static final String TABLE_NEIGHBORHOODS = "Neighborhoods";
// Common column names
private static final String KEY_ID = "_id";
// TABLE_COUNTIES Table - column names
private static final String COL_COUNTYDETAIL = "countyDetail";
// TABLE_DESCRIPTIONS Table - column names
private static final String COL_DESCRIPTIONDETAIL = "descriptionDetail";
// TABLE_NEIGHBORHOODS Table - column names
private static final String COL_NEIGHBORHOODDETAIL = "neighborhoodDetail";
private SQLiteDatabase myDataBase;
private final Context myContext;
public static long INSERT_ERROR = -1;
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
*/
public database(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.myContext = context;
}
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase() throws IOException{
Log.d(LOG, "Calling checkDataBase() Method");
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
Log.d(LOG, "!!!Database Found!!!");
}else{
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
Log.d(LOG, "!!!Creating Empy Database!!!");
this.getReadableDatabase();
this.close();
try {
Log.d(LOG, "!!!Coping Database!!!");
copyDataBase();
} catch (IOException e) {
throw new Error(e);
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
Log.d(LOG, "looking database at " + myPath);
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
Log.e(LOG, "Exception: database does't exist yet");
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
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;
Log.d(LOG, "Coping database from " + myInput + ", to " + outFileName);
//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();
}
public void openDataBase() throws SQLException{
openDataBase(true);
}
public void openDataBase(boolean readonly) throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
if (readonly)
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
else
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) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
我希望这有帮助。祝你好运
为了兼容性,您可能会将内部db路径更改为
if(android.os.Build.VERSION.SDK_INT >= 17) {
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
} else {
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
}
问题内容: 我想在应用程序的第一次运行中将一个很大的目录从我的应用程序的Assets文件夹复制到data文件夹。我怎么做?我已经尝试过一些示例,但是没有用,所以我什么都没有。我的目标是Android 4.2。 谢谢,Yannik 问题答案: 尝试使用您的Application实例的以下代码(您应该在清单中编写该类):该代码将资产/文件文件夹的内容复制到应用程序的缓存文件夹中(您可以将其他路径放在c
问题内容: 我尝试从资产目录复制SQLite数据库,以便以后访问。但是我做不到! DatabaseHelper很简单: 尝试了一切!我尝试玩扩展!但是我仍然收到一个错误:复制数据库时出错: 我检查了模拟器,我的文件在那里,所以我应该可以写它!请任何帮助!真让我发疯! UPD 我试图将其放在SD卡上,并且可以正常工作。但仍然无法理解为什么我无法将其写入应用程序数据文件夹。 问题答案: 我使用此助手,
我是Android的新手。 我已经尝试了这个论坛的所有帖子。但未能取得成功。我正在尝试将mp3文件从asset文件夹共享到whatsapp。 下面是我的代码。 这是我在主活动中的代码: ContentProvider中的代码: 清单文件:
问题内容: 我正在尝试将资产发送到我的Sony SmartWatch3。我遵循了Google的文档(https://developer.android.com/training/wearables/data- layer/assets.html ),但是它不起作用。 我的掌上电脑活动的代码: 我的可穿戴活动的代码: 手持式代码的执行工作正常(正确显示了日志),但是该程序未输入 onDataChan
你知道我做错了什么吗?