如何在Android存储中写入/读取文件夹/文件 [英] How to write/read a folder/file in android storage

查看:104
本文介绍了如何在Android存储中写入/读取文件夹/文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在使用以下方法在android外部存储设备中创建路径.

Currently, I'm using below method to create a path in android external storage.

public void createPath() {
    String path = Environment.getExternalStorageDirectory().getPath()+"/myFolder/";
    boolean exists = (new File(path)).exists();
    if (!exists) {
        new File(path).mkdirs();
    }
}

据我了解,getExternalStorageDirectory()并不专门指外部存储.因此,此方法将在用户首选的存储(通常是内部存储)中创建myFolder.

As far as I understand getExternalStorageDirectory() does not exclusively refer to external storage. So this method will create myFolder in user preferred storage (which is generally internal storage).

现在我有一个问题:

如果默认存储(用户首选存储)已设置为SD Card并且无法读取/写入,会发生什么情况?处理这种情况的最佳方法是什么?

What happens if the default storage(user preferred storage) has been set to SD Card and it's not readable/writable? What is the best way to handle this situation?

换句话说,如何安全地创建此文件夹?(内部或外部都无所谓).

In another word, How can I create this folder safely?(Internal or External doesn't matter ).

推荐答案

据我了解,getExternalStorageDirectory()并不专门指外部存储.

As far as I understand getExternalStorageDirectory() does not exclusively refer to external storage.

是的,确实如此,因为Android SDK称为

Yes, it does, for what the Android SDK refers to as external storage.

因此,此方法将在用户首选的存储(通常是内部存储)中创建myFolder.

So this method will create myFolder in user preferred storage (which is generally internal storage).

不,Android SDK指的是内部存储空间是另一回事.

No, what the Android SDK refers to as internal storage is something else.

如果默认存储(用户首选存储)已设置为SD卡并且无法读取/写入,会发生什么情况?

What happens if the default storage(user preferred storage) has been set to SD Card and it's not readable/writable?

除非我误解了您的描述,否则Android本身没有用户首选存储"的概念.

Android itself does not have a concept of "user preferred storage", unless I am misinterpreting your description.

如何安全创建此文件夹?

How can I create this folder safely?

您的代码似乎很好.

只有极少数设备(外部设备存储在可移动媒体上)(我的猜测:〜0.1%).这是Android 1.x/2.x的主要模型,但是这些设备已不再使用,并且大多数较新的设备都在板载闪存上具有内部和外部存储.如果用户弹出可移动媒体,则您将无权访问外部存储. Environment上有一些获取外部存储状态的方法.但是,您就没有地方在用户可以访问它们的地方写文件了.您的选择将是:

There is a very small percentage of devices (my guess: ~0.1%) where external storage is on removable media. This was the dominant model for Android 1.x/2.x, but those devices are not used much anymore, and most newer devices have internal and external storage both on on-board flash. If the user ejected the removable media, you will not have access to external storage. There are methods on Environment for getting the external storage state. However, you then have no place to write your files where the user can access them. Your choices would be:

  • 将文件存储在内部存储中,然后在介质再次可用时稍后将它们迁移到外部存储中

  • Store the files on internal storage, then migrate them to external storage at a later time, when the media is available again

请勿存储任何内容,请用户再次使外部存储可用

Do not store anything, asking the user to please make external storage available again

这篇关于如何在Android存储中写入/读取文件夹/文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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