Android Can';t将数据库复制到SD卡
我正在开发一个使用数据库的Android。我想在使用true machine运行时将数据库复制到SD卡。但它并不总是成功的。我的程序没有说明原因,只是无法复制数据库。我将数据库存储在Android Can';t将数据库复制到SD卡,android,Android,我正在开发一个使用数据库的Android。我想在使用true machine运行时将数据库复制到SD卡。但它并不总是成功的。我的程序没有说明原因,只是无法复制数据库。我将数据库存储在res\raw\mobilephone1.db中;它是10MB。我设置了允许将数据写入SD卡的权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> &l
res\raw\mobilephone1.db
中;它是10MB。我设置了允许将数据写入SD卡的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
是这样吗
private final String DATABASE_PATH = android.os.Environment
.getExternalStorageDirectory().getAbsolutePath()
+ "/mobilephone";
private final String DATABASE_FILENAME = "mobilephone.db";
private SQLiteDatabase openDatabase()
{
try
{
// obtain absolute path of mobilephone.db
String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
File dir = new File(DATABASE_PATH);
// if/sdcard/mobilephone exist,crtate this list
if (!dir.exists()) {
dir.mkdir();
}
// if mobilephone.db file did not exist in/sdcard/mobilephone then copy this filt to SD card list(/sdcard/mobilephone) from res\raw
if (!(new File(databaseFilename)).exists())
{
// obtain the InputStream object that encapsulate mobilephone.db
InputStream is = getResources().openRawResource(R.raw.mobilephone1);
FileOutputStream fos = new FileOutputStream(databaseFilename);
int count = 0;
while (count == 0) {
count = is.available();
}
byte[] buffer = new byte[count];
int readCount = 0; // the number of bytes that has successfully readen
while (readCount < count) {
readCount += is.read(buffer, readCount, count - readCount);
}
fos.write(buffer, 0, count);
fos.close();
is.close();
}
// go to mobilephone.db in /sdcard/mobilephone
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
databaseFilename, null);
return database;
}
catch (Exception e)
{
}
return null;
}
private final String数据库\u PATH=android.os.Environment
.getExternalStorageDirectory().getAbsolutePath()
+“/手机”;
私有最终字符串数据库\u FILENAME=“mobilephone.db”;
私有SQLiteDatabase openDatabase()
{
尝试
{
//获取mobilephone.db的绝对路径
字符串databaseFilename=数据库路径+“/”+数据库文件名;
文件目录=新文件(数据库路径);
//如果存在/sdcard/mobilephone,请注明此列表
如果(!dir.exists()){
dir.mkdir();
}
//如果/sdcard/mobilephone中不存在mobilephone.db文件,则将此过滤器从res\raw复制到SD卡列表(/sdcard/mobilephone)
如果(!(新文件(数据库文件名)).exists())
{
//获取封装mobilephone.db的InputStream对象
InputStream=getResources().openRawResource(R.raw.mobilephone1);
FileOutputStream fos=新的FileOutputStream(数据库文件名);
整数计数=0;
而(计数=0){
count=is.available();
}
字节[]缓冲区=新字节[计数];
int readCount=0;//已成功读取的字节数
while(readCount
试试这个:
File sd = Environment.getExternalStorageDirectory();
File src = new File("YOUR_SOURCE_PATH");
File dest = new File(sd + "/" +DATABASE_NAME);
try
{
if (Environment.getExternalStorageDirectory().canWrite())
{
if (srcdb.exists())
{
FileChannel src = new FileInputStream(srcdb).getChannel();
FileChannel dst = new FileOutputStream(destdb).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
flag=0;
}
else
{
Toast.makeText(getApplicationContext(), "ERROR: File not found in Database " , Toast.LENGTH_LONG).show();
}
}
else
{
Toast.makeText(getApplicationContext(), "ERROR: Cannot write to file" , Toast.LENGTH_LONG).show();
}
}
catch (Exception e)
{
Log.e("Movedb", "Error in Copying" + e.toString());
}
此外,您只需要一个权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
e.printstacktrace和check打印异常。一旦您告诉我SD卡中有可用空间,我将查看您的代码。正如nandeesh所说,异常细节会告诉你确切的原因。谢谢@nandeesh我找到了问题谢谢@con_9我找到了问题