把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数据库
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数据库
解决方案 »
- 如何实现在没有网络的情况下能够读取上次浏览过的新闻
- 有没有人遇见过这种情况???有没有人遇见过这种情况???有没有人遇见过这种情况???有没有人遇见过这种情况???有没有人遇见过这种情况???
- 求救:在google地图开发上覆盖gif图标实现动态效果
- android中实现收藏夹功能,在线等
- 那位大侠了解android日历模块 讲讲 谢谢!!!!!!!!!!!!
- Android 如何用sql语句来操作系统库
- android如何设计大量信息录入
- ConcurrentModificationException 错误
- 初学android,关于minSdkVersion有点疑问
- 怎么通过.java去生成控件啊?
- RemoteViewsService 定时刷新
- 据说是没有关闭cursor ,但是我这么写,怎么还是报错啊
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
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);
}
}