android 导出和导入sqlite数据库问题

发布于 2025-01-12 13:49:26 字数 2073 浏览 0 评论 0原文

我的导出和导入(用户备份)的实现代码有问题。

我尝试了下面的代码,但找不到创建的文件夹,也找不到复制的数据库。代码的目的是为用户提供此选项来导出其设置并将其保存在可访问的位置。我希望拥有这种能力,如果用户删除应用程序,备份文件保留在手机上,或者至少用户应该能够将其复制到其他地方或共享它。 有人可以检查我的代码并让我知道它有什么问题吗?

public class UserBackupActivity extends BaseActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.addContentView(R.layout.activity_user_backup);

    if (ContextCompat.checkSelfPermission(UserBackupActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(UserBackupActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            ActivityCompat.requestPermissions(UserBackupActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 5);
        }
    } else {
        File mydir = new File(getExternalFilesDir("")+ "/mydir/");


        if (!mydir.exists()){
            mydir.mkdirs();

        }

        else
            Log.d("error", "dir. already exists");


        exportDB();
        // importDB();

    }
}


private void exportDB() {
    String DatabaseName = "user.db";
    File sd = getExternalFilesDir("");
    File data = Environment.getDataDirectory();



    FileChannel source = null;
    FileChannel destination = null;
    String currentDBPath = "/data/" +getPackageName()+ "/databases/" + DatabaseName;
    String backupDBPath = "/mydir/mydatabase";
    File currentDB = new File(data, currentDBPath);
    File backupDB = new File(sd, backupDBPath);



    try {
        source = new FileInputStream(currentDB).getChannel();
        destination = new FileOutputStream(backupDB).getChannel();
        destination.transferFrom(source, 0, source.size());
        source.close();
        destination.close();
        Toast.makeText(this, "Your Database is Exported !!", Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

当我运行代码时,我得到“您的数据库已导出!!”消息,但我在模拟器上找不到数据库文件。我的目标 android Api 是 31。

谢谢。

I have problem with implementation code for export and import (backup for users ) on my.

I tried the below code but I couldn't find the created folder and also copied database. the the purpose of the code is give the user this option to export his settings and save them somewhere accessible. and I want to have this ability if user remove the app the backup file remain on the phone or at least user should be able to copy it somewhere else or share it .
Could someone check my code and let me know what is the problem of it ?

public class UserBackupActivity extends BaseActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.addContentView(R.layout.activity_user_backup);

    if (ContextCompat.checkSelfPermission(UserBackupActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(UserBackupActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            ActivityCompat.requestPermissions(UserBackupActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 5);
        }
    } else {
        File mydir = new File(getExternalFilesDir("")+ "/mydir/");


        if (!mydir.exists()){
            mydir.mkdirs();

        }

        else
            Log.d("error", "dir. already exists");


        exportDB();
        // importDB();

    }
}


private void exportDB() {
    String DatabaseName = "user.db";
    File sd = getExternalFilesDir("");
    File data = Environment.getDataDirectory();



    FileChannel source = null;
    FileChannel destination = null;
    String currentDBPath = "/data/" +getPackageName()+ "/databases/" + DatabaseName;
    String backupDBPath = "/mydir/mydatabase";
    File currentDB = new File(data, currentDBPath);
    File backupDB = new File(sd, backupDBPath);



    try {
        source = new FileInputStream(currentDB).getChannel();
        destination = new FileOutputStream(backupDB).getChannel();
        destination.transferFrom(source, 0, source.size());
        source.close();
        destination.close();
        Toast.makeText(this, "Your Database is Exported !!", Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

when I run the code I got "Your Database is Exported !!" message but I can not find the database file on emulator . also my target android Api is 31.

thanks .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文