有关android /data/data/ [

Ronys 2013-04-26 01:34:49
把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();
...全文
694 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
他在 if (path.mkdirs()){ Log.i("test", "创建成功"); }else{ Log.i("test", "创建失败"); }; 这里报错
  • 打赏
  • 举报
回复
我的也是,我的路径是对的,也加了权限,他运行有时可以,有时不行 org.crosswalkproject.Navigation37abcCrossWalk是我的包名 String filePath ="data/data/org.crosswalkproject.Navigation37abcCrossWalk/zhiyue.db"; String pathStr = "data/data/org.crosswalkproject.Navigation37abcCrossWalk/"; SQLiteDatabase database; public SQLiteDatabase openDatabase(Context context){ File jhPath=new File(filePath); if(jhPath.exists()){ return SQLiteDatabase.openOrCreateDatabase(jhPath, null); }else{ File path=new File(pathStr); Log.i("test", "pathStr="+path); if (path.mkdirs()){ Log.i("test", "创建成功"); }else{ Log.i("test", "创建失败"); }; try { AssetManager am= context.getAssets(); InputStream is=am.open("zhiyue.db"); FileOutputStream fos=new FileOutputStream(jhPath); byte[] buffer=new byte[1024]; int count = 0; while((count = is.read(buffer))>0){ fos.write(buffer,0,count); } fos.flush(); fos.close(); is.close(); } catch (IOException e) { e.printStackTrace(); return null; } return openDatabase(context); } }
csdn_2013 2013-04-26
  • 打赏
  • 举报
回复
引用 楼主 weiyirong 的回复:
把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);
		
	}
	
}

  • 打赏
  • 举报
回复
是/data/data/package_name/databases/xx.db 另外,看看权限开了吧
顾小林 2013-04-26
  • 打赏
  • 举报
回复
你那个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
Ronys 2013-04-26
  • 打赏
  • 举报
回复
结贴了!确实路径错了

80,472

社区成员

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

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