.如何将文件从项目资产文件夹复制到android studio中的设备数据文件夹? [英] .How to copy files from project assets folder to device data folder in android studio?

查看:39
本文介绍了.如何将文件从项目资产文件夹复制到android studio中的设备数据文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SQLiteAssetHelper,它要求我在 PC 上创建数据库并将其复制到 assets/databases/databasename.db.我这样做了,但是当我检查设备时,我的应用程序的数据文件夹下也不存在数据库和数据库文件夹.

I am trying to use SQLiteAssetHelper and it requires me to create database on PC and copy it to assets/databases/databasename.db. I do that, but when I check on the device, nor the database, nor the databases folder is there under my app's data folder.

我是根据视频做的,那里的人没有做任何其他事情来使复制工作.

I did this based on a video, and the guy there doesn't do anything else to make the copy work.

那么,文件是否应该自动复制到设备上?如果没有,我如何复制文件?

So, are the files supposed to be automatically copied over to device? If not, how can I copy the files?

推荐答案

那么,文件是否应该自动复制到设备上?如果没有,我如何复制文件?

So, are the files supposed to be automatically copied over to device? If not, how can I copy the files?

是的,但只有在尝试访问数据库时才会这样做.只是实例化扩展 SQLiteAssetHelper 的类不会复制数据库,因此如果你只做这些,那么数据库将不可见.

Yes it will BUT only when an attempt is made to access the database. Just instantiating the class that extends SQLiteAssetHelper will not copy the database and thus if that is all you do then the database will not be visible.

使用SQliteAssetHelper的步骤应该是:-

The steps when using SQliteAssetHelper to take should be :-

  1. 创建您的项目.
  2. 修改 build.gradle 以在依赖项部分包含 implementation 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'.
  3. 在assets文件夹中创建assets文件夹,然后创建databases文件夹和
  4. 将数据库文件复制到数据库文件夹中.
  5. 创建扩展 SQLiteAssetHelper 的 Database Helper 类.
  6. 实例化数据库助手
  7. 访问数据库(这是复制数据库的时间).
  1. Create your project.
  2. Amend the build.gradle to include implementation 'com.readystatesoftware.sqliteasset:sqliteassethelper:+' in the dependencies section.
  3. Create the assets folder and then the databases folder in the assets folder and
  4. Copy the database file into the databases folder.
  5. Create the Database Helper class that extends SQliteAssetHelper.
  6. Instantiate the Database Helper
  7. Access the database (this is when the database is copied).

如果在执行第 5 步时使用,作为示例(基于问题中的信息):-

If when doing step 5 you use, as an example (based upon the information in the question) :-

public class DatabaseHelper extends SQLiteAssetHelper {

    public static final String DATABASENAME = "databasename.db"; //<<<<<<<<< MUST match file name in assets/databases
    public static final int DATABASEVERSION = 1;

    public DatabaseHelper(Context context) {
        super(context, DATABASENAME, null, DATABASEVERSION);
        this.getWritableDatabase(); //<<<<< will force database access and thus copy database from assets
    }
}

然后当做 6(实例化数据库助手)时,那么 7 就完成了,因为 this.getWritableDatabase(); 访问(隐式打开)数据库.

Then when doing 6 (instantiating the database helper), then 7 is done because this.getWritableDatabase(); accesses (implicitly opens) the database.

例如

public class MainActivity extends AppCompatActivity {

    DatabaseHelper mDBHelper; //<<<<< declares the database helper

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDBHelper = new DatabaseHelper(this); // instantiates the database helper
    }
}

这篇关于.如何将文件从项目资产文件夹复制到android studio中的设备数据文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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