将数据库文件复制到android中的sdcard

我通过此代码获取我的数据库文件
File dbFile=getDatabasePath("EdsysEyfsDB.db");
               Log.v("database name checking",dbFile.toString());

我想把这个数据库文件复制到sdcard中,所以我可以为此做一些操作.但是我不能做任何操作.以下代码用于复制到SD卡

if (dbFile.exists()) {
                       InputStream inStream = new FileInputStream(dbFile);
                       String file = Environment.getExternalStorageDirectory().getPath()
                            +"/" + "database.db";
                       Log.d("file name checking in  dbFilecondition",file);
                       FileOutputStream fs = new FileOutputStream(file);
                       byte[] buffer = new byte[1444];
                       while ((byteread = inStream.read(buffer)) != -1) {
                           bytesum += byteread;
                           fs.write(buffer,byteread);
                       }
                       inStream.close();
                       fs.close();
                   }

但我不会在这种情况下.数据库文件名在LogCat上正确.我已经允许读写文件.

解决方法

尝试这个希望,这有助于你
public void exportDatabse(String databaseName) {
        try {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();

            if (sd.canWrite()) {
                String currentDBPath = "//data//"+getPackageName()+"//databases//"+databaseName+"";
                String backupDBPath = "backupname.db";
                File currentDB = new File(data,currentDBPath);
                File backupDB = new File(sd,backupDBPath);

                if (currentDB.exists()) {
                    FileChannel src = new FileInputStream(currentDB).getChannel();
                    FileChannel dst = new FileOutputStream(backupDB).getChannel();
                    dst.transferFrom(src,src.size());
                    src.close();
                    dst.close();
                }
            }
        } catch (Exception e) {

        }
    }

如何调用

exportDatabse("Yourdbname");

注意 :

记住要添加写入外部存储的权限< uses-permission android:name =“android.permission.WRITE_EXTERNAL_STORAGE”/>否则sd.canWrite()将为false.

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 &#39;EastRiver&#39; 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...