从android设备sd导出数据库无法写入

时间:2015-07-02 16:42:15

标签: android sqlite export

你好我有这个代码

public void onbtnExportDBToPCClick(View view){

    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        Toast.makeText(getBaseContext(), data.toString(),
                Toast.LENGTH_LONG).show();
        if (sd.canWrite()) {
            String currentDBPath = "//"+data+"//" + getPackageName() + "//databases//"+"cDataBase.db";
            String backupDBPath = "//backupcDataBase.db";
            File currentDB = new File(currentDBPath);
            File backupDB = new File(sd, backupDBPath);
            Toast.makeText(getBaseContext(), currentDBPath.toString(),
                    Toast.LENGTH_LONG).show();

            if (currentDB.exists()) {
                FileChannel src = new FileInputStream(currentDB).getChannel();
                FileChannel dst = new FileOutputStream(backupDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
                Toast.makeText(getBaseContext(), backupDB.toString(),
                        Toast.LENGTH_LONG).show();

            }
        }
    } catch (Exception e) {

        Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
                .show();

   }

我已经设置了permitions

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" > </uses-permission>

但代码在if(sd.canWrite())处停止,它不会更进一步。我不知道是什么问题。我还应该设置什么?

1 个答案:

答案 0 :(得分:0)

link将有助于澄清一些问题。

您需要使用android studio中提供的终端在虚拟磁盘映像上配置SD卡空间。

基本的shell命令似乎是这样的:

mksdcard -l mySdCard 1024M mySdCardFile.img

编辑:

或者,如果您对shell不熟悉,只需转到AVD管理器并编辑模拟器的高级设置即可。我认为该选项类似于SD卡,然后选择工作室管理,然后提供X mb的内存。

相关问题