问题描述
这个问题已经被问的人,但经历从许多人的步骤,我仍然无法在我的结束来解决这个问题,我的应用程序工作,但不是真正的设备上模拟器罚款,并给出以下2例外,第一个例外是第二例外的主要来源,我想有一些权限问题。我已经添加了
<使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAGE/>
要清单文件,但我得到了同样的结果。我的数据库是很简单的一个表。此外,我已经添加了android_metadata表和编辑我的其他表的id字段为_id。我相信我的数据库是在资产的文件夹present。
任何帮助将是AP preciated。
第一个例外
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):无法打开Shajra写(会尝试只读):
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):com.readystatesoftware.sqliteasset.SQLiteAssetHelper $ SQLiteAssetException:缺少数据库/ Shajra文件(或.zip,.gz的压缩文件)的资产,或目标文件夹不可写
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):在android.content.res.AssetManager.openAsset(本机方法)
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):在android.content.res.AssetManager.open(AssetManager.java:325)
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):在android.content.res.AssetManager.open(AssetManager.java:299)
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):在com.readystatesoftware.sqliteasset.SQLiteAssetHelper.copyDatabaseFromAssets(SQLiteAssetHelper.java:436)
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):在com.readystatesoftware.sqliteasset.SQLiteAssetHelper.createOrOpenDatabase(SQLiteAssetHelper.java:400)
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):在com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getWritableDatabase(SQLiteAssetHelper.java:176)
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):在com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getReadableDatabase(SQLiteAssetHelper.java:254)
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):在com.nannandotcom.MainActivity.onCreate(MainActivity.java:27)
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):在android.app.ActivityThread.access $ 1500(ActivityThread.java:135)
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1054)
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):在android.os.Handler.dispatchMessage(Handler.java:99)
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):在android.os.Looper.loop(Looper.java:150)
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):在android.app.ActivityThread.main(ActivityThread.java:4389)
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):在java.lang.reflect.Method.invokeNative(本机方法)
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):在java.lang.reflect.Method.invoke(Method.java:507)
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:849)
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
12月2日至24日:58:10.067:E / SQLiteAssetHelper(7107):在dalvik.system.NativeStart.main(本机方法)
第二个例外
12月2日至24日:58:10.077:E /数据库(7107):dbopen():sqlite3_open_v2(/数据/数据/ com.example.nannandotcom /数据库/ Shajra ,&安培;手柄,1,NULL)失败
12月2日至24日:58:10.077:E /数据库(7107):dbopen():错误号= 2,错误消息=没有这样的文件或目录
12月2日至24日:58:10.077:D / AndroidRuntime(7107):关闭VM
12月2日至24日:58:10.077:W / dalvikvm(7107):主题ID = 1:螺纹未捕获的异常(组= 0x4011f5a0)退出
12月2日至24日:58:10.108:E / AndroidRuntime(7107):致命异常:主要
12月2日至24日:58:10.108:E / AndroidRuntime(7107):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.nannandotcom / com.nannandotcom.MainActivity}:android.database.sqlite.SQLiteException:不能打开数据库文件
12月2日至24日:58:10.108:E / AndroidRuntime(7107):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
12月2日至24日:58:10.108:E / AndroidRuntime(7107):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
12月2日至24日:58:10.108:E / AndroidRuntime(7107):在android.app.ActivityThread.access $ 1500(ActivityThread.java:135)
12月2日至24日:58:10.108:E / AndroidRuntime(7107):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1054)
12月2日至24日:58:10.108:E / AndroidRuntime(7107):在android.os.Handler.dispatchMessage(Handler.java:99)
12月2日至24日:58:10.108:E / AndroidRuntime(7107):在android.os.Looper.loop(Looper.java:150)
12月2日至24日:58:10.108:E / AndroidRuntime(7107):在android.app.ActivityThread.main(ActivityThread.java:4389)
12月2日至24日:58:10.108:E / AndroidRuntime(7107):在java.lang.reflect.Method.invokeNative(本机方法)
12月2日至24日:58:10.108:E / AndroidRuntime(7107):在java.lang.reflect.Method.invoke(Method.java:507)
12月2日至24日:58:10.108:E / AndroidRuntime(7107):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:849)
12月2日至24日:58:10.108:E / AndroidRuntime(7107):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
12月2日至24日:58:10.108:E / AndroidRuntime(7107):在dalvik.system.NativeStart.main(本机方法)
12月2日至24日:58:10.108:E / AndroidRuntime(7107):android.database.sqlite.SQLiteException:无法打开数据库文件产生的原因
12月2日至24日:58:10.108:E / AndroidRuntime(7107):在android.database.sqlite.SQLiteDatabase.dbopen(本机方法)
12月2日至24日:58:10.108:E / AndroidRuntime(7107):在android.database.sqlite.SQLiteDatabase<&初始化GT;(SQLiteDatabase.java:1962)
12月2日至24日:58:10.108:E / AndroidRuntime(7107):在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:906)
12月2日至24日:58:10.108:E / AndroidRuntime(7107):在com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getReadableDatabase(SQLiteAssetHelper.java:264)
12月2日至24日:58:10.108:E / AndroidRuntime(7107):在com.nannandotcom.MainActivity.onCreate(MainActivity.java:27)
12月2日至24日:58:10.108:E / AndroidRuntime(7107):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
12月2日至24日:58:10.108:E / AndroidRuntime(7107):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
12月2日至24日:58:10.108:E / AndroidRuntime(7107):11 ...更多
DatabaseHelper
包com.nannandotcom;进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStream中;进口android.content.Context;
进口android.database.Cursor;
进口android.database.sqlite.SQLiteDatabase;进口com.nannandotcom.model.ProfileBean;
进口com.readystatesoftware.sqliteasset.SQLiteAssetHelper;公共类DatabaseHelper扩展SQLiteAssetHelper { 私有静态最后弦乐DATABASE_NAME =Shajra;
私有静态最终诠释DATABASE_VERSION = 1; 私人上下文的背景下; 公共DatabaseHelper(上下文的背景下){
超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
this.context =背景;
} 公共无效copyDatabase()抛出IOException
// SQLiteDatabase CHECKDB = SQLiteDatabase.openDatabase(context.getAssets()+(DATABASE_NAME),空,SQLiteDatabase.OPEN_READONLY);
// checkDB.close();
InputStream的myInput = context.getAssets()开(DATABASE_NAME)。
字符串outFileName =/数据/数据/+ context.getPackageName()+/数据库/+ DATABASE_NAME;
的OutputStream myOutput =新的FileOutputStream(outFileName); 字节[]缓冲区=新的字节[1024];
INT长;
而((长度= myInput.read(缓冲液))大于0){
myOutput.write(缓冲液,0,长度);
} myOutput.flush();
myOutput.close();
myInput.close();
} 公共ProfileBean getProfileBean(SQLiteDatabase数据库,INT ID){
ProfileBean profileBean =新ProfileBean();
尝试{
光标C = database.query(情景模式
新的String [] {_ ID,姓名,资格,WorkingAt,LivesAt,MaritalStatus,NumberOfChildren
电话,电子邮件,图片,MarriedTo,父亲,母亲,出生日期,性别},
_id =?,
新的String [] {Integer.toString(ID)},NULL,NULL,NULL);
如果(C!= NULL){
如果(c.moveToFirst()){
做{ }而(c.moveToNext());
}
}
}
赶上(例外五){
e.printStackTrace();
}
返回profileBean;
}
}
MainActivity
包com.nannandotcom;进口java.io.IOException异常;
进口com.example.nannandotcom.R;进口android.os.Bundle;
进口android.app.Activity;
进口android.content.Intent;
进口android.database.Cursor;
进口android.database.SQLException;
进口android.database.sqlite.SQLiteDatabase;
进口android.util.Log;
进口android.view.Menu;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.Toast;公共类MainActivity延伸活动{ @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
DatabaseHelper databaseHelper =新DatabaseHelper(本);
SQLiteDatabase数据库= databaseHelper.getReadableDatabase(); 尝试{
databaseHelper.copyDatabase();
}
}赶上(的SQLException SQLE){ 扔SQLE; }
}
赶上(例外五){
Log.e(MainActivity,e.getMessage());
e.printStackTrace();
} 最终的EditText enterName =(EditText上)findViewById(R.id.editText1);
按钮提交=(按钮)findViewById(R.id.button1);
submit.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(视图v){
意图mainActivityIntent =新意图(MainActivity.this,Welcome.class);
mainActivityIntent.putExtra(username的,enterName.getText()的toString());
startActivity(mainActivityIntent);
}
});
} @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}}
我认为你缺少分贝扩展在 DATABASE_NAME
。
所以,给出具体的 DATABASE_NAME
带扩展名按资产目录。
替换
私有静态最后弦乐DATABASE_NAME =Shajra;
通过
私有静态最后弦乐DATABASE_NAME =Shajra.db;
或者
私有静态最后弦乐DATABASE_NAME =Shajra.sqlite;
This question has already been asked by the people but going through the steps from many of them I am still unable to fix the problem at my end, My app is working fine on emulator but not on the real device and gives the following 2 exceptions, the first exception is the major source of second exception and i guess there is some permission issue. I have added the
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
to the manifest file but i got the same result. My database is very simple with one table. ALso i have added "android_metadata" table and edit my other table's id field to "_id". and I am sure my database is present in the assets folder.
Any help would be appreciated.
first exception
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): Couldn't open Shajra for writing (will try read-only):
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): com.readystatesoftware.sqliteasset.SQLiteAssetHelper$SQLiteAssetException: Missing databases/Shajra file (or .zip, .gz archive) in assets, or target folder not writable
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): at android.content.res.AssetManager.openAsset(Native Method)
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): at android.content.res.AssetManager.open(AssetManager.java:325)
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): at android.content.res.AssetManager.open(AssetManager.java:299)
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.copyDatabaseFromAssets(SQLiteAssetHelper.java:436)
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.createOrOpenDatabase(SQLiteAssetHelper.java:400)
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getWritableDatabase(SQLiteAssetHelper.java:176)
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getReadableDatabase(SQLiteAssetHelper.java:254)
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): at com.nannandotcom.MainActivity.onCreate(MainActivity.java:27)
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): at android.app.ActivityThread.access$1500(ActivityThread.java:135)
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): at android.os.Looper.loop(Looper.java:150)
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): at android.app.ActivityThread.main(ActivityThread.java:4389)
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): at java.lang.reflect.Method.invoke(Method.java:507)
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
02-24 12:58:10.067: E/SQLiteAssetHelper(7107): at dalvik.system.NativeStart.main(Native Method)
second exception
02-24 12:58:10.077: E/Database(7107): dbopen(): sqlite3_open_v2("/data/data/com.example.nannandotcom/databases/Shajra", &handle, 1, NULL) failed
02-24 12:58:10.077: E/Database(7107): dbopen(): errno = 2, error message = No such file or directory
02-24 12:58:10.077: D/AndroidRuntime(7107): Shutting down VM
02-24 12:58:10.077: W/dalvikvm(7107): threadid=1: thread exiting with uncaught exception (group=0x4011f5a0)
02-24 12:58:10.108: E/AndroidRuntime(7107): FATAL EXCEPTION: main
02-24 12:58:10.108: E/AndroidRuntime(7107): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nannandotcom/com.nannandotcom.MainActivity}: android.database.sqlite.SQLiteException: unable to open database file
02-24 12:58:10.108: E/AndroidRuntime(7107): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
02-24 12:58:10.108: E/AndroidRuntime(7107): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
02-24 12:58:10.108: E/AndroidRuntime(7107): at android.app.ActivityThread.access$1500(ActivityThread.java:135)
02-24 12:58:10.108: E/AndroidRuntime(7107): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
02-24 12:58:10.108: E/AndroidRuntime(7107): at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 12:58:10.108: E/AndroidRuntime(7107): at android.os.Looper.loop(Looper.java:150)
02-24 12:58:10.108: E/AndroidRuntime(7107): at android.app.ActivityThread.main(ActivityThread.java:4389)
02-24 12:58:10.108: E/AndroidRuntime(7107): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 12:58:10.108: E/AndroidRuntime(7107): at java.lang.reflect.Method.invoke(Method.java:507)
02-24 12:58:10.108: E/AndroidRuntime(7107): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
02-24 12:58:10.108: E/AndroidRuntime(7107): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
02-24 12:58:10.108: E/AndroidRuntime(7107): at dalvik.system.NativeStart.main(Native Method)
02-24 12:58:10.108: E/AndroidRuntime(7107): Caused by: android.database.sqlite.SQLiteException: unable to open database file
02-24 12:58:10.108: E/AndroidRuntime(7107): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
02-24 12:58:10.108: E/AndroidRuntime(7107): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1962)
02-24 12:58:10.108: E/AndroidRuntime(7107): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:906)
02-24 12:58:10.108: E/AndroidRuntime(7107): at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getReadableDatabase(SQLiteAssetHelper.java:264)
02-24 12:58:10.108: E/AndroidRuntime(7107): at com.nannandotcom.MainActivity.onCreate(MainActivity.java:27)
02-24 12:58:10.108: E/AndroidRuntime(7107): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
02-24 12:58:10.108: E/AndroidRuntime(7107): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
02-24 12:58:10.108: E/AndroidRuntime(7107): ... 11 more
DatabaseHelper
package com.nannandotcom;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.nannandotcom.model.ProfileBean;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
public class DatabaseHelper extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "Shajra";
private static final int DATABASE_VERSION = 1;
private Context context;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
public void copyDatabase() throws IOException{
// SQLiteDatabase checkDB = SQLiteDatabase.openDatabase(context.getAssets().+(DATABASE_NAME), null, SQLiteDatabase.OPEN_READONLY);
// checkDB.close();
InputStream myInput = context.getAssets().open(DATABASE_NAME);
String outFileName = "/data/data/"+ context.getPackageName() + "/databases/" + DATABASE_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
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 ProfileBean getProfileBean(SQLiteDatabase database,int id){
ProfileBean profileBean = new ProfileBean();
try {
Cursor c = database.query("Profiles",
new String[]{"_id","Name","Qualification","WorkingAt","LivesAt","MaritalStatus","NumberOfChildren",
"Telephone","Email","Picture","MarriedTo","Father","Mother","DOB","Gender"},
"_id=?",
new String[]{Integer.toString(id)},null, null, null);
if (c != null ) {
if (c.moveToFirst()) {
do {
}while (c.moveToNext());
}
}
}
catch(Exception e){
e.printStackTrace();
}
return profileBean;
}
}
MainActivity
package com.nannandotcom;
import java.io.IOException;
import com.example.nannandotcom.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseHelper databaseHelper = new DatabaseHelper(this);
SQLiteDatabase database = databaseHelper.getReadableDatabase();
try {
databaseHelper.copyDatabase();
}
}catch(SQLException sqle){
throw sqle;
}
}
catch(Exception e){
Log.e("MainActivity", e.getMessage());
e.printStackTrace();
}
final EditText enterName = (EditText) findViewById(R.id.editText1);
Button submit = (Button) findViewById(R.id.button1);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent mainActivityIntent = new Intent(MainActivity.this, Welcome.class);
mainActivityIntent.putExtra("userName", enterName.getText().toString());
startActivity(mainActivityIntent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
i think you are missing db extension in DATABASE_NAME
.
So give specific DATABASE_NAME
with extension as per assets directory.
Replace
private static final String DATABASE_NAME = "Shajra";
With
private static final String DATABASE_NAME = "Shajra.db";
Or
private static final String DATABASE_NAME = "Shajra.sqlite";
这篇关于SQLiteAssetHelper:无法打开数据库写入(会尝试只读)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!