把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数据库
解决方案 »
- android 按Home切回到程序后都做了什么?
- ksoap2 呼叫带参数的.Net Webservice问题
- 关于gridview
- 关于intent-filter的问题,请教大家
- 求解Android嵌套线性布局,我的第二个线性布局出不来,求助牛人。。。
- opencore如何编译生成.so库
- 基于Android平台的手机本地文件安全问题
- 通过Intent去打开weixin时,报签名错.是不是要事先签名?
- android串口通信
- 运行android项目报错,找不到/.android/avd
- 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);
}
}