将SQLite数据库保存在Android平板电脑内部存储器(API 23)中 [英] Saving a SQLite database in Android tablet internal memory (API 23)

查看:172
本文介绍了将SQLite数据库保存在Android平板电脑内部存储器(API 23)中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android应用程序开发中的一位新手。但是,我当前的项目要求我开发一个记录现场观察的应用程序(一名研究科学家),并将其保存到SQLite数据库中-这些将被发送到中央位置进行处理。该应用程序将在平板电脑上使用。

I am an abject novice in Android app development. However, my current project requires me to make an app that records field observations (am a research scientist), save them to a SQLite database - these will be sent to a central location for processing. The app will be used on tablets.

当前,我使用的是Android Studio,当我模拟应用程序功能时,一切按计划进行,并使用用于SQLite的数据库浏览器,我可以看到这些值已正确记录在数据库中。

Currently, I am using Android Studio, and when I emulate the app functions, everything works as planned and using DB Browser for SQLite, I can see that the values are being recorded correctly in the database.

进行研究(例如在此处)的答案,我知道数据库( .db )文件保存在目录路径中:

Doing research (an example is the answer here), I am aware that the the database (.db) file is saved in the directory path:

/ data / data /< .app.package> /databases/foo.db

当然,这可以在Android Studio中访问,但是我的理解是,除非设备是根目录,这些文件在实际设备中不可访问。出于各种原因,这不是我参与的工作的选择。

Of course, this is accessible in Android Studio, but my understanding is that unless the device is rooted, these files are inaccessible in an actual device. For various reasons, this is not an option for the work that I am involved with.

在研究中,我遇到了一种备份数据库并保存数据库的方法。将数据库备份到SD卡,如问题在Android中备份/还原sqlite db的操作[重复] 重复的目标 Android备份/还原:如何备份内部数据库?

In researching, I came across a method to back up the database, and save the back up database to an SD card, as in the answer to the question Backup/restore sqlite db in android [duplicate] and the duplicate target Android backup/restore: how to backup an internal database?.

要保存到外部存储设备(例如,使用23及更高版本的API的SD卡),现在需要更多的写外部源权限(超出清单中始终要求的权限)。由于许多原因,该选项不是最佳选择。

Saving to external storage devices such as SD cards in APIs of 23 and above now require considerable more permissions to write to external sources (above and beyond what has always been required in the Manifest). For many reasons, this option is not optimal.

此答案中,备份保存在SD卡上:

In this answer, the backup is saved on the SD card:

final String inFileName = "/data/data/<your.app.package>/databases/foo.db";
File dbFile = new File(inFileName);
FileInputStream fis = new FileInputStream(dbFile);

String outFileName = Environment.getExternalStorageDirectory()+"/database_copy.db";

// Open the empty db as the output stream
OutputStream output = new FileOutputStream(outFileName);

// Transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer))>0){
    output.write(buffer, 0, length);
}

// Close the streams
output.flush();
output.close();
fis.close();

但是,我的问题(可能是人为屁的类型)是-是否有可能将备份保存到设备的内部文件(例如,Documents文件夹)中,并且仍然需要权限吗?

But, my question (possibly a brain-fart type) is - is it possible to have the backup saved to an internal file of the device (e.g. Documents folder), and would the permissions still be required?

推荐答案

实现准入流程并不复杂。 这是我为寻求帮助而创建的类<​​/a>。您必须从中扩展MainActivity,然后可以调用:

It's not complicated to achieve the pemission's flow. Here is a class that I created for help. You have to extend your MainActivity from this, and then you can call:

verifyStoragePermissions(new Runnable() {
    @Override
    public void run() {
        //backup your db here...
    }
});

它适用于所有使用android支持库的Android版本。

It works for all Android versions using android support library.

这篇关于将SQLite数据库保存在Android平板电脑内部存储器(API 23)中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆