在 Android 中保存文件 - 初学者(内部/外部存储) [英] Saving Files in Android - For Beginners (Internal / External Storage)

本文介绍了在 Android 中保存文件 - 初学者(内部/外部存储)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 android 开发很陌生,我需要初学者的帮助...

I'm very new to android development and I need a beginners help please...

我正在创建一个应用程序,它应该有 2 个目录,一个 Databases 目录和一个 Images 目录,就像 Whatsapp 一样.

I am creating an app that should have 2 directories, a Databases directory and an Images directory, just like Whatsapp has.

我想在浏览手机时在文件管理器中看到这些目录,就像您看到其他应用程序文件夹一样.我尝试了在这里找到的所有内容,从代码在内部存储和外部存储中创建它.文件夹似乎已创建,但是当我浏览手机的文件管理器时,我找不到包含我的应用程序名称的文件夹以及其中的数据库和图像文件夹...我究竟做错了什么?我需要在 android studio 中创建这些文件夹作为添加文件夹吗?还是我需要从代码中创建它?

I want to see those directories in the File Manager when I'm browsing my phone, just like you see other apps folders. I tried everything I found here, to create it in the Internal storage and in the External storage from code. The folders seem to be created, but when I browsed my phone's File Manager, I couldn't find the folder with my app name and inside it my Databases and Images folders... What am I doing wrong? do I need to create those folders in the android studio as adding folders? or do I need to create it from code?

能否请您告诉我完成此任务所需的操作?我正在使用 android studio 3.1.3.感谢帮助者!:)

Can you please give me the actions I need to do to accomplish this task? I'm using android studio 3.1.3. Thanks for the helpers! :)

推荐答案

内部存储"和外部存储"这两个术语一开始可能会令人困惑,因为 Google 的意图与我们期望的不同从我们日常使用的语言中知道:外部"并不一定意味着SD 卡".这个人写了一篇关于术语混淆的精彩文章

The terms "Internal Storage" and "External Storage" might be confusing at first, because Google's intentions are different from what we would expect & know from our day-to-day use of language: "External" doesn't necessarily mean the "SD Card". This guy made a great article about the terminology confusion

根据您的意图,您希望使用外部存储概念.文档 中很好地解释了这些差异,但我将简要介绍给你.

According to your intentions, you'd want to be working with the External Storage concept. The differences are well explained in the Documentation, but I'll shortly brief them to you here.

最后我会给你一个例子,但首先让我们了解基础知识:

At the end I'll provide you an example, but first lets know the basics:

内部存储

  • 文件只能由您的应用
  • 访问
  • 卸载您的应用时会删除文件
  • 文件始终可用(这意味着它们的文件永远不会保存在可移动内存中)

外部存储

  • 其他应用(包括文件管理器应用的任何变体,就您而言)完全可以读取文件
  • 卸载您的应用后不一定会删除文件 - 稍后解释
  • 无法保证文件可用性(可以被其他应用程序/可移动内存删除)
  • Files are fully readable by other apps (including any variant of File Manager app, in your case)
  • Files aren't necessarily removed when your app is uninstalled - explained later
  • Files availability isn't guaranteed (can be deleted by other apps / removable memory)

既然我们知道您需要外部存储,那么在开始之前需要做几件事:

So now that we know you need External Storage, there are several things needed to be done before starting:

  • Require Permissions(读/写)在您的 Manifest.xml 文件中,具体取决于您的需要:
  • Require Permissions (read/write) inside your Manifest.xml file, depending on your needs:
    <manifest ...>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    ...
    </manifest>

每个权限都是独立的,这意味着您不需要同时拥有两个权限,例如,如果您只想读取文件而不是写入文件

Each permission stands by its own, meaning you don't need to have both if, for example, you only wish to read files instead of writing them

  • 验证存储可用- 这是在运行时完成的,并且在文档中得到了很好的解释.我们需要确保存储已安装到设备中/其状态不会以某种方式导致读/写请求失败.

在给定的方法中,我们将在根目录中保存一个文本文件.

In the given method, we will save a text file inside the root directory.

感谢这篇文章

public void writeFileExternalStorage() {

    //Text of the Document
    String textToWrite = "bla bla bla";

    //Checking the availability state of the External Storage.
    String state = Environment.getExternalStorageState();
    if (!Environment.MEDIA_MOUNTED.equals(state)) {

        //If it isn't mounted - we can't write into it.
        return;
    }

    //Create a new file that points to the root directory, with the given name:
    File file = new File(getExternalFilesDir(null), filenameExternal);

    //This point and below is responsible for the write operation
    FileOutputStream outputStream = null;
    try {
        file.createNewFile();
        //second argument of FileOutputStream constructor indicates whether
        //to append or create new file if one exists
        outputStream = new FileOutputStream(file, true);

        outputStream.write(textToWrite.getBytes());
        outputStream.flush();
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

<小时>

我想特别回答您的一些问题:


I'd like to answer specifically to some of your questions:

我是否需要在 android studio 中创建这些文件夹作为添加文件夹?还是我需要从代码中创建它?

do I need to create those folders in the android studio as adding folders? or do I need to create it from code?

绝对不是通过 Android Studio.这些是您的项目文件夹,其中包含您的代码.上面提到了这样做的方法.

Definitely not via the Android Studio. These are your projects folder, containing your code. The way to do it is mentioned above.

我找不到包含我的应用程序名称的文件夹以及其中的数据库和图像文件夹...我做错了什么?

I couldn't find the folder with my app name and inside it my Databases and Images folders... What am I doing wrong?

可能将您的文件保存为内部存储文件/如前所述将它们保存为项目文件夹 - 而那些不会(也不应该)显示.

Probably saved your files as Internal Storage ones / saved them as project folders as you mentioned earlier - and those wouldn't (and shouldn't) show up.

要知道的有用信息

目录有两种类型:公共目录和私有目录.

There are 2 types of directories: public and private.

私人

  • 媒体商店无法访问
  • 卸载应用程序时删除文件
  • 通过getExternalFilesDir(...)方法获取

示例: WhatsApp 目录(在我的手机中)位于根级别.调用它会是:getExternalFilesDir("WhatsApp/...")

Example: the WhatsApp directory (in my phone) is located right at the root level. Calling it would be: getExternalFilesDir("WhatsApp/...")

公开(下载/电影/图片库)

Public (Downloads/Movies/Images libraries)

  • 文件由 MediaStore
  • 扫描
  • 通过Environment.getExternalStoragePublicDirectory(...)方法获取

示例: 获取 Documents 文件夹将如下所示:Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)

Example: getting the Documents folder would look like: Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)

这篇关于在 Android 中保存文件 - 初学者(内部/外部存储)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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