且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Android存储中写入/读取文件夹/文件

更新时间:2023-02-07 11:29:08

据我了解,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