AndroidStudio下读取assets下db文件报错

CaptainXero 2016-04-07 11:10:07
Hi,以下问题困扰我很久,求大神们不吝赐教!
首先我是用的IDE是AndroidStudio 1.5.1 ,API 23 (Android 6.0)
在读取assets下的db文件时出现问题,以下是我的相关代码以及报错提示:
ublic class AntiVirusDao {
private static String DB_PATH = "";
private static String DB_NAME = "antivirus.db";
public static String checkVirus(String md5,Context context){
String desc = null;
/**打开病毒MD5数据库*/
SQLiteDatabase db = SQLiteDatabase.openDatabase
(DB_PATH = context.getApplicationInfo().dataDir + "/"+DB_NAME,null,SQLiteDatabase.OPEN_READWRITE);
Cursor cursor = db.rawQuery("select desc from datable where md5=?",new String[]{md5});
if(cursor.moveToNext()){
desc = cursor.getString(0);
}
cursor.close();
db.close();
return desc;
}

new Thread(){
public void run(){
Message msg = Message.obtain();
msg.what = SCAN_BEGIN;
myHandler.sendMessage(msg);//sengEmptyMessage只能存放整形数据,sendMessage可以存放其他类型
List<PackageInfo> installedPackage = pm.getInstalledPackages(0);
total = installedPackage.size();
for(PackageInfo info : installedPackage){
if(!flag){
isStop = true;
return;
}
String apkpath = info.applicationInfo.sourceDir;
String md5info = MD5Utils.getFileMD5(apkpath);
// context = getApplicationContext();
String result = AntiVirusDao.checkVirus(md5info,mcontext);
msg = Message.obtain();
msg.what = SCANNING;
ScanAppInfo scanAppInfo = new ScanAppInfo();
if(result==null){
scanAppInfo.description = "您的手机安全";
scanAppInfo.isVirus = false;
}else {
scanAppInfo.description = result;
scanAppInfo.isVirus = true;
}
process++;
scanAppInfo.packageName = info.packageName;
scanAppInfo.appName = info.applicationInfo.loadLabel(pm).toString();
msg.obj = scanAppInfo;
msg.arg1 = process;
myHandler.sendMessage(msg);
try{
Thread.sleep(300);
}catch (InterruptedException e){
e.printStackTrace();
}
}
msg = Message.obtain();
msg.what = SCAN_FINISH;
myHandler.sendMessage(msg);
};
}.start();
}

...全文
602 5 打赏 收藏 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
CaptainXero 2016-04-07
  • 打赏
  • 举报
回复
引用 2 楼 CaptainXero 的回复:
[quote=引用 1 楼 jklwan 的回复:] 读取assets下的文件用InputStream is = getAssets().open("文件名");
Hi,感谢你的回答,请问能否针对我这个写几行代码我看一下,因为还不是很清楚。谢谢[/quote] 谢谢,我突然明白了,问题解决了,感谢!
CaptainXero 2016-04-07
  • 打赏
  • 举报
回复
引用 3 楼 jklwan 的回复:
[quote=引用 2 楼 CaptainXero 的回复:] Hi,感谢你的回答,请问能否针对我这个写几行代码我看一下,因为还不是很清楚。谢谢
想要使用assets下的db数据库,你需要把文件拷贝到你知道的目录下,然后再读取,参考代码

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();
 
    }
拷贝完毕以后就可以用SQLiteDatabase去读取DB_PATH + DB_NAME的数据库了。[/quote] 谢谢您的回答,对我非常有帮助。请问这个DB_PATH是我随意指定?我自己声明一个路径? 比如我自己定义 String DB_PATH = “/data/data/com.example.administrator.security/databases/” 然后去读 DB_PATH+“antivirus.db” 这样对么?新手,望不吝赐教
jklwan 2016-04-07
  • 打赏
  • 举报
回复
引用 2 楼 CaptainXero 的回复:
Hi,感谢你的回答,请问能否针对我这个写几行代码我看一下,因为还不是很清楚。谢谢
想要使用assets下的db数据库,你需要把文件拷贝到你知道的目录下,然后再读取,参考代码

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();
 
    }
拷贝完毕以后就可以用SQLiteDatabase去读取DB_PATH + DB_NAME的数据库了。
CaptainXero 2016-04-07
  • 打赏
  • 举报
回复
引用 1 楼 jklwan 的回复:
读取assets下的文件用InputStream is = getAssets().open("文件名");
Hi,感谢你的回答,请问能否针对我这个写几行代码我看一下,因为还不是很清楚。谢谢
jklwan 2016-04-07
  • 打赏
  • 举报
回复
读取assets下的文件用InputStream is = getAssets().open("文件名");

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧