您可以写信给SD卡的文件夹时,有没有SD卡? [英] Can you write to the sdcard folder when there is no sdcard?

查看:126
本文介绍了您可以写信给SD卡的文件夹时,有没有SD卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我测试上4.1星系S3的应用程序,运行2.3的旧手机。无论是手机中有一个SD卡,但是当我使用 Environment.getExternalStorageDirectory()。getAbsolutePath()在我的星系S3它引导我到一个叫做文件夹,名为内的存储文件夹sdcard0 。当我上了年纪的手机上运行它,它给我的路径MNT / SD卡/和SD卡的文件夹是只读的。

So I'm testing an app on a galaxy s3 on 4.1 and an older phone that runs 2.3. Neither phone has an sdcard in it but when I use Environment.getExternalStorageDirectory().getAbsolutePath() on my galaxy s3 it directs me to a folder called sdcard0 within a folder called storage. When I run it on the older phone it gives me the path mnt/sdcard/ and the sdcard folder is read only.

我想创建在这些目录中的文件夹。我可以做我S3但不是我旧的手机上。

I am trying to create a folder in these directories. I can do it on my s3 but not on my older phone.

有没有类似我失去了旧的手机上或者我可以写SD卡文件夹中存储文件夹东西的时候有没有SD卡present?

Is there something similar to the storage folder I'm missing on the older phone or can I write to the sdcard folder when there is no sd card present?

编辑:我在我的清单中的外部写入权限

I have the external write permissions in my manifest

推荐答案

在某些手机上,作为<一个href=\"http://developer.android.com/reference/android/os/Environment.html#getExternalStorageState%28%29\"相对=nofollow>文档表明,这个词的外部的不采取一切太严重了:

On some phones, as the documentation suggests, the word external is not to be taken all too serious:

注意:不要被这个词外部在这里混淆:此
  目录可以更好地被认为是媒体/共享存储。 这是一个
  文件系统,可容纳相对大量的数据
和中
  在所有应用程序共享(不强制权限)。
  的传统这是SD卡,但它也可以被实现为内置存储在一个设备,它是从保护不同
  内部存储和可安装在计算机上的文件系统。

Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

您可以检查存储的真正的外部与否,使用 Environment.isExternalStorageRemovable() -method 。如果它是不可拆卸的,你应该总是能够写入它(假设你拥有的权限声明)。

You can check if the storage is really external or not, using the Environment.isExternalStorageRemovable()-method. If it's not removable, you should always be able to write to it (given that you have the permissions declared).

如果它的的可拆卸不过,你需要检查它的当前状态,<一个href=\"http://developer.android.com/reference/android/os/Environment.html#getExternalStorageState%28%29\"相对=nofollow> Environment.getExternalStorageState() 。下面是如何使用它的一个简单的例子:

If it is however removable, you'll need to check it's current state with Environment.getExternalStorageState(). Here's a quick example of how to use it:

String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
    // We can read and write!
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    // Mounted read only...
} else {
    // Something is wrong...
}

因此​​,在这种情况下,你可以写信给外部存储。

So in these cases, you can write to the external storage.

重要提示:由于文档指定的多个点,上述方法在主要外部存储工作。某些设备(如摩托罗拉Xoom),同时拥有一个内部的外部的存储和SD卡。

Important: As the documentation specifies at multiple points, the above methods work for the primary external storage. Some devices (like the Motorola Xoom) have both an internal "external" storage, and an SD-Card.

有似无的通用的方式来访问二次的外部存储在这些设备上,虽然有一个的标准现在,与Android 4.2引入了

There seems to be no "generalized" way to access the "secondary external storage" on such devices, although there is a standard now, introduced with Android 4.2

设备可以包含外部存储的多个实例,但
  目前只有主外部存储通过API暴露给开发者。

Devices may contain multiple instances of external storage, but currently only the primary external storage is exposed to developers through API.

[...]

在Android 4.2开始,设备可以支持多个用户,
  外部存储必须满足以下约束

Starting in Android 4.2, devices can support multiple users, and external storage must meet the following constraints:


      
  • [...]

  •   
  • 二次外部存储不得被应用程序可写。

  •   
  • [...]
  • Secondary external storage must not be writable by apps.

这似乎取决于供应商的存储(内部或的真正的外部)被认为是主要的。没有多少,你可以在这里做(没有的事情越来越乱......)

It seems to depend on the vendor which storage (internal or real external) is considered the primary one. Not much you can do here (without things getting messy...)

如果没有外部存储present,你可以使用你的应用程序的内部存储,或者缓存目录,这取决于你要存储什么样的数据。

If there is no external storage present, you can use your applications Internal Storage, or the caching directory, depending on what kind of data you're storing.

这篇关于您可以写信给SD卡的文件夹时,有没有SD卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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