首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

相干android /data/data/ [

2013-06-25 
有关android /data/data/ [把asset下的数据库xx.db复制到/data/data/package_name/xx.db下,无法复制啊 !!

有关android /data/data/ [
把asset下的数据库xx.db复制到/data/data/package_name/xx.db下,无法复制啊 !!什么问题 ?? 应该不用root吧? 这个目录本身是属于本应用程序的!理论上来说本应用程序是可以访问!
private SQLiteDatabase openDataBase(String path) { 
Log.e("SQLite exist:", "" + new File(path).exists() + "::" + path); 
// TODO Auto-generated method stub 
try { 
File file = new File(path); 
if (!file.exists()) {// 判断数据库是否存在,若不存在执行导入操作,否则直接打开数据库 
// InputStream ins = context.getResources().openRawResource( 
// R.raw.ty_city); 
file.createNewFile(); 
InputStream ins = context.getAssets().open("Chinacity.db"); 
FileOutputStream fos = new FileOutputStream(path); 
byte[] buffer = new byte[BUFFER_SIZE]; 
int count = 0; 
while ((count = ins.read(buffer)) > 0) { 
fos.write(buffer, 0, count); 
Log.e("buffer...", "" + count); 

fos.close(); 
ins.close(); 

SQLiteDatabase sqLiteDatabase = context.openOrCreateDatabase( 
DB_NAME, 0, null); 
return sqLiteDatabase; 
} catch (FileNotFoundException ffe) { 
ffe.printStackTrace(); 
Log.e("DataBaseFile:", "file can't find"); 
} catch (IOException ioe) { 
// TODO: handle exception 
Log.e("IOException", ioe.getMessage()); 
ioe.printStackTrace(); 

return null; 
}
报错

java.io.IOException: open failed: ENOENT (No such file or directory)//file.createNewFile(); 
Android 数据库
[解决办法]
你那个path 或许方法不对

abstract File getFileStreamPath(String name)
Returns the absolute path on the filesystem where a file created with openFileOutput(String, int) is stored.
http://developer.android.com/reference/android/content/Context.html
[解决办法]
是/data/data/package_name/databases/xx.db

另外,看看权限开了吧
[解决办法]

引用:
把asset下的数据库xx.db复制到/data/data/package_name/xx.db下,无法复制啊 !!什么问题 ?? 应该不用root吧? 这个目录本身是属于本应用程序的!理论上来说本应用程序是可以访问!
private SQLiteDatabase openDataBase(String path) { 
Log.e("SQLite exist:", "" + new File……



import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.tarena.android.db.DatabaseHelper;
import com.tarena.android.entity.Airport;

public class AirportReadActivity extends Activity{



private ListView lv;
private DatabaseHelper dbHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.airport_list);

dbHelper=new DatabaseHelper(this);

lv=(ListView)findViewById(R.id.airport_list_view);

List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();

AssetManager asset=this.getAssets();
InputStream is=null;
BufferedReader br=null;
FileOutputStream fos=null;
PrintWriter pw=null;

try {
is=asset.open("airport.txt");
                        br=new BufferedReader(new InputStreamReader(is,"GBK"));
fos=new FileOutputStream("/data/data/com.android.test/airport_1.txt");
pw=new PrintWriter(fos);

String str=null;
while((str=br.readLine())!=null){

String[] ss=str.split(":");
Airport airport=new Airport();
airport.setId(Integer.parseInt(ss[0]));
airport.setCity(ss[3]);
if(ss.length>5){
airport.setName(ss[5]);
}else{
airport.setName(ss[3]);
}
airport.setCode(ss[1]);
data.add(airport.transferToMap());
dbHelper.insert(airport);

pw.println(str);
pw.flush();
}
} catch (IOException e) {
// TODO: handle exception
}finally{
                        if(br!=null)try{br.close();}catch(IOException e){}
if(is!=null)try{is.close();}catch(IOException e){}
if(pw!=null) pw.close();
if(fos!=null)try{fos.close();}catch(IOException e){}

}

SimpleAdapter adapter=new SimpleAdapter(this,data,R.layout.airport_list_item,
new String[]{"airport_id","airport_city","airport_name","airport_code"},
new int[]{R.id.airport_id_tv,R.id.airport_city_tv,R.id.airport_name_tv,R.id.airport_code_tv});

lv.setAdapter(adapter);

}

}

热点排行