在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.

浏览手机时,我想在文件管理器中看到这些目录,就像您看到其他应用程序文件夹一样. 我尝试了在这里找到的所有内容,并通过代码在内部存储和外部存储中创建了它.该文件夹似乎已创建,但是当我浏览手机的文件管理器时,找不到包含我的应用程序名称的文件夹,并且在其中没有我的Databases and Images文件夹... 我究竟做错了什么?我需要在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:

  • 需要权限(读/写)在您的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.

我找不到包含我的应用程序名称的文件夹,并且在其中没有我的Databases和Images文件夹...我在做什么错了?

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(...)方法检索
  • Not accessible by the Media Store
  • Files are removed when app is uninstalled
  • Retrieved by getExternalFilesDir(...) method

示例: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(...)方法检索
  • Files are scanned by the MediaStore
  • Retrieved by Environment.getExternalStoragePublicDirectory(...) method

示例:获得Documents文件夹的外观如下:Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)

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

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

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