如何从android工程raw文件夹下把数据库读到SD卡里去?
答案:2 悬赏:30
解决时间 2021-04-08 18:15
- 提问者网友:巴黎塔下许过得承诺
- 2021-04-08 15:19
如何从android工程raw文件夹下把数据库读到SD卡里去?
最佳答案
- 二级知识专家网友:陪伴是最长情的告白
- 2021-04-08 15:49
public static boolean copyDataBase(Context con)
{
try
{
String databaseFilenames = PATH + dbName;
File dir = new File(DATABASE_PATH);
//判断文件夹是否存在,不存在就新建一个
if (!dir.exists())
{
dir.mkdirs();
}
File dbf = new File(DATABASE_PATH + dbName);
if (dbf.exists())
{
dbf.delete();
}
FileOutputStream os = null;
os = new FileOutputStream(databaseFilenames);//得到数据库文件的写入流
InputStream is = con.getResources().openRawResource(R.raw.xxx);//得到数据库文件的数据流
byte[] buffer = new byte[1024];
int count = 0;
while ((count = is.read(buffer)) > 0)
{
os.write(buffer, 0, count);
}
os.flush();
os.close();
is.close();
return true;//复制文件成功
}
catch (SQLiteException e)
{
return false;
}
catch (FileNotFoundException e1)
{
e1.printStackTrace();
return false;
}
catch (IOException e)
{
e.printStackTrace();
return false;
}
}
{
try
{
String databaseFilenames = PATH + dbName;
File dir = new File(DATABASE_PATH);
//判断文件夹是否存在,不存在就新建一个
if (!dir.exists())
{
dir.mkdirs();
}
File dbf = new File(DATABASE_PATH + dbName);
if (dbf.exists())
{
dbf.delete();
}
FileOutputStream os = null;
os = new FileOutputStream(databaseFilenames);//得到数据库文件的写入流
InputStream is = con.getResources().openRawResource(R.raw.xxx);//得到数据库文件的数据流
byte[] buffer = new byte[1024];
int count = 0;
while ((count = is.read(buffer)) > 0)
{
os.write(buffer, 0, count);
}
os.flush();
os.close();
is.close();
return true;//复制文件成功
}
catch (SQLiteException e)
{
return false;
}
catch (FileNotFoundException e1)
{
e1.printStackTrace();
return false;
}
catch (IOException e)
{
e.printStackTrace();
return false;
}
}
全部回答
- 1楼网友:我的任性你不懂
- 2021-04-08 17:09
这是你格式化的时候选错了,如果是品牌的话最好的办法是将原卡送回售后,让他们帮您修正!
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯