TAG: cannot open file at line 30191
在开发手机安全卫士的过程中,遇到一个BUG困扰了我一整天。大概就是在打开病毒MD5值数据库时遇到问题,无法打开这个db。起初以为是在Android Studio下assets目录下的文件使用与以前的Eclipse不同,就各种改路径,后来发现是拷贝数据库到手机的过程出了问题。
##解决办法
*使用assets目录下的数据库,必须将数据库拷贝过来后方可使用。
*在读取assets下的文件用InputStream is = getAssets().open("文件名")读取;
详细的报错和源码,以及修改后的代码如下:
1.报错内容
2.相关代码
'ublicclassAntiVirusDao {
privatestaticString DB_PATH ="";
privatestaticString DB_NAME ="antivirus.db";
publicstaticString 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=?",newString[]{md5});
if(cursor.moveToNext()){
desc = cursor.getString(0);
}
cursor.close();
db.close();
returndesc;
}'
'newThread(){
publicvoidrun(){
Message msg = Message.obtain();
msg.what = SCAN_BEGIN;
myHandler.sendMessage(msg);//sengEmptyMessage只能存放整形数据,sendMessage可以存放其他类型
List 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 =newScanAppInfo();
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();
}'
3.最终解决后的代码