这是将文件保存到内存的安全且正确的方法吗?

发布于 2024-12-11 14:30:25 字数 1446 浏览 0 评论 0 原文

我一直在使用以下代码,但直觉上我认为它不太安全。它能在所有设备上完美运行吗?此代码在第一次运行应用程序时将我的主数据库 (100 kB) 复制到内存中。感谢您抽出时间。

private void CopyDBtoInternal() {

    String path = "/data/data/com.myproject/databases/";
    String DB_DESTINATION = "/data/data/com.myproject/databases/sample.db";

    // Check if the database exists before copying
    boolean initialiseDatabase = (new File(DB_DESTINATION)).exists();
    if (initialiseDatabase == false) {

        //create folders
        File db_folder = new File(path);
        db_folder.mkdirs();

        AssetManager assetManager = getApplicationContext().getAssets();

        try {
            InputStream is = assetManager.open("sample.db");

            // Copy the database into the destination
            OutputStream os = new FileOutputStream(DB_DESTINATION);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = is.read(buffer)) > 0){
                os.write(buffer, 0, length);
            }
            os.flush();

            os.close();
            is.close();

        } catch (IOException e) {
            e.printStackTrace();
            AlertDialog.Builder dialogo = new AlertDialog.Builder(AppGuiaDeBulas.this);
            dialogo.setTitle("Alerta");
            dialogo.setMessage("It was not possible to save database. Error #001");
            dialogo.setNeutralButton("OK", null);
            dialogo.show();
        }
    }

}

I've been using the following code, but intuitively I see it's not too safe. Will it work flawlessly in every device? This code copies to internal memory my main database (100 kB) in the first app run. Thanks for your time.

private void CopyDBtoInternal() {

    String path = "/data/data/com.myproject/databases/";
    String DB_DESTINATION = "/data/data/com.myproject/databases/sample.db";

    // Check if the database exists before copying
    boolean initialiseDatabase = (new File(DB_DESTINATION)).exists();
    if (initialiseDatabase == false) {

        //create folders
        File db_folder = new File(path);
        db_folder.mkdirs();

        AssetManager assetManager = getApplicationContext().getAssets();

        try {
            InputStream is = assetManager.open("sample.db");

            // Copy the database into the destination
            OutputStream os = new FileOutputStream(DB_DESTINATION);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = is.read(buffer)) > 0){
                os.write(buffer, 0, length);
            }
            os.flush();

            os.close();
            is.close();

        } catch (IOException e) {
            e.printStackTrace();
            AlertDialog.Builder dialogo = new AlertDialog.Builder(AppGuiaDeBulas.this);
            dialogo.setTitle("Alerta");
            dialogo.setMessage("It was not possible to save database. Error #001");
            dialogo.setNeutralButton("OK", null);
            dialogo.show();
        }
    }

}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她说她爱他 2024-12-18 14:30:25

查看文章中有关写入存储的部分。

如果您使用的是 API 级别 8 (Froyo) 或更高版本,请使用 getExternalFilesDir() 打开一个文件,该文件表示您应在其中保存文件的外部存储目录。此方法采用一个类型参数来指定所需的子目录类型,例如 DIRECTORY_MUSIC 和 DIRECTORY_RINGTONES(传递 null 以接收应用程序文件目录的根目录)。如果需要,此方法将创建适当的目录。通过指定目录类型,您可以确保 Android 的媒体扫描仪能够正确对系统中的文件进行分类(例如,铃声被识别为铃声而不是音乐)。如果用户卸载您的应用程序,该目录及其所有内容都将被删除。

如果您使用的是 API 级别 7 或更低版本,请使用 getExternalStorageDirectory (),打开一个代表外部存储根目录的文件。然后,您应该将数据写入以下目录:

/Android/data//files/
这是您的 Java 风格的包名称,例如“com.example.android.app”。如果用户的设备运行 API 级别 8 或更高版本并且他们卸载您的应用程序,则该目录及其所有内容都将被删除。

http://developer.android.com/guide/topics/data /data-storage.html#AccessingExtFiles

Check out this part of the article on writing to storage.

If you're using API Level 8 (Froyo) or greater, use getExternalFilesDir() to open a File that represents the external storage directory where you should save your files. This method takes a type parameter that specifies the type of subdirectory you want, such as DIRECTORY_MUSIC and DIRECTORY_RINGTONES (pass null to receive the root of your application's file directory). This method will create the appropriate directory if necessary. By specifying the type of directory, you ensure that the Android's media scanner will properly categorize your files in the system (for example, ringtones are identified as ringtones and not music). If the user uninstalls your application, this directory and all its contents will be deleted.

If you're using API Level 7 or lower, use getExternalStorageDirectory(), to open a File representing the root of the external storage. You should then write your data in the following directory:

/Android/data//files/
The is your Java-style package name, such as "com.example.android.app". If the user's device is running API Level 8 or greater and they uninstall your application, this directory and all its contents will be deleted.

http://developer.android.com/guide/topics/data/data-storage.html#AccessingExtFiles

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文