简体   繁体   English

Environment.getExternalStorageDirectory()无法正常工作

[英]Environment.getExternalStorageDirectory() not working

I'm trying to create a file in the external storage (SD Card) of my phone. 我正在尝试在手机的外部存储(SD卡)中创建文件。

File F = new File(Environment.getExternalStorageDirectory() + File.separator + "MyBrand" + File.separator + "MyApp" + File.separator + "MyFile.srl");

but this is creating it in the Internal Storage area. 但这是在内部存储区域中创建的。 I checked in Windows Explorer. 我检查了Windows资源管理器。 When debugging, the path of F is "/mtn/sdcard/MyBrand/MyApp/MyFile.srl", which is wrong because in Windows Explorer the name of the sdcard is really "SD Card" (with a space). 调试时, F的路径为“ /mtn/sdcard/MyBrand/MyApp/MyFile.srl”,这是错误的,因为在Windows资源管理器中,sdcard的名称实际上是“ SD卡”(带有空格)。 I also tried .getAbsolutePath at the end of getExternalStorageDirectory()` but it did the exact same thing. 我还在getExternalStorageDirectory()的末尾尝试了.getAbsolutePath ,但它确实做了同样的事情。

My code for creating this file: 我创建此文件的代码:

if (F.exists() == false)
{
  try
  {
    F.getParentFile().getParentFile().mkdir();
    F.getParentFile().mkdir();
    F.createNewFile();
  }
  catch (IOException e)
  {
    e.printStackTrace();
  }

  scr_save();
}
else
  scr_load();

It does the mkdirs() and createNewFile() just fine, but scr_save and scr_load always throw exceptions, either FileNotFound or IOException. 它确实执行了mkdirs()和createNewFile(),但是scr_save和scr_load总是抛出FileNotFound或IOException异常。

EDIT: I've also been testing it when not plugged into my computer, just to make sure of no interference. 编辑:当没有插入计算机时,我也一直在进行测试,以确保没有干扰。 To be safe, i always delete the paths and file before unplugging it. 为了安全起见,我总是在拔出路径和文件之前将其删除。 It seems to create the dirs and file, but then not reading it right (could it be not serialized properly? i use this file to store serialized objects). 它似乎创建了目录和文件,但随后却无法正确读取(可能无法正确序列化吗?我使用此文件存储序列化的对象)。

EDIT: there was an error originally. 编辑:最初有一个错误。 I was putting in the path + filename as a single parameter to F. Now I pass the path and filename as 2 separate strings. 我将路径+文件名作为F的单个参数输入。现在我将路径和文件名作为2个单独的字符串传递。 However, i'm still experiencing the same problem :( 但是,我仍然遇到同样的问题:(

I have the correct permission in my manifest (.WRITE_EXTERNAL_STORAGE) 我在清单(.WRITE_EXTERNAL_STORAGE)中具有正确的权限

I use Windows 7 64-bit, and also the 64-bit version of Eclipse. 我使用Windows 7 64位以及Eclipse的64位版本。 My phone is a LG-MS770. 我的手机是LG-MS770。

Anyway please help. 无论如何请帮助。 No clue why the getExternalSorage is not working. 不知道为什么getExternalSorage无法正常工作。

I'm trying to create a file in the external storage (SD Card) of my phone 我正在尝试在手机的外部存储(SD卡)中创建文件

What the Android SDK refers to as "external storage" is not an SD card, on most devices manufactured since early 2011. 自2011年初以来制造的大多数设备上,Android SDK所称的“外部存储”不是SD卡。

No clue why the getExternalSorage is not working. 不知道为什么getExternalSorage无法正常工作。

It is working just fine and as documented . 正如文件所述,它工作正常。 It is simply not meeting your expectations. 这根本达不到您的期望。

Up until Android 4.4, there was no documented and supported means of accessing any sort of removable storage. 直到Android 4.4为止,尚无记录和受支持的访问任何类型的可移动存储的方法。 On Android 4.4, you now have getExternalFilesDirs() and getExternalCacheDirs() (note the plural), which may give you access to specific directories on removable storage if such storage exists. 在Android 4.4上,您现在具有getExternalFilesDirs()getExternalCacheDirs() (请注意复数),如果存在此类存储,则可以访问可移动存储上的特定目录。

First of all, /mnt/sdcard/MyBrand/MyApp/MyFile.srl is correct: SD Card can be a label assigned by Windows Explorer. 首先,/ /mnt/sdcard/MyBrand/MyApp/MyFile.srl是正确的: SD Card可以是Windows资源管理器分配的标签。
Anyway, take a look at getExternalStorageDirectory documentation where it says: 无论如何,请查看getExternalStorageDirectory文档 ,其中显示:

This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened. 如果用户已将其安装在计算机上,已从设备中删除或发生了其他问题,则该目录当前可能无法访问。 You can determine its current state with getExternalStorageState(). 您可以使用getExternalStorageState()确定其当前状态。

Likely, you debug when the SD Card is mounted and this is wrong. 可能会在mounted SD Card时进行调试 ,这是错误的。
Then, mkdir does not throw exception if it fails; 然后,如果mkdir失败,则不会引发exception createNewFile should do it, but when you read something like "or some other problem has happened" , you should prepare yourself for headaches. createNewFile应该这样做,但是当您读到诸如“或发生了其他问题”之类的消息时 ,您应该为头痛做准备。

The getExternalStorage is pretty confusing. getExternalStorage非常令人困惑。 It does not represent the SD card you plug in your phone, but the content that will be displayed on your computer when you plug your phone in. 它不代表您插入手机中的SD卡,而是代表您插入手机时将在计算机上显示的内容。

This is the Storage you can browse to put some images, music, etc. on your phone or get them from your phone. 这是一个存储空间,您可以浏览以在手机上放置一些图像,音乐等或从手机上获取它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 到 Environment.getExternalStorageDirectory() 还是不? - To Environment.getExternalStorageDirectory() or not to? Android如何正确使用Environment.getExternalStorageDirectory() - Android how to use Environment.getExternalStorageDirectory() corretly 关于“ Environment.getExternalStorageDirectory()。getAbsolutePath()”的语法 - The syntax about “Environment.getExternalStorageDirectory().getAbsolutePath()” Environment.getExternalStorageDirectory()找不到文件android - Environment.getExternalStorageDirectory() not finding file android 使用Environment.getExternalStorageDirectory()。getAbsolutePath()设置路径 - Using Environment.getExternalStorageDirectory().getAbsolutePath() for setting path 比较现有字符串与Environment.getExternalStorageDirectory()。getPath()的问题 - Issue with comparing existing string to Environment.getExternalStorageDirectory().getPath() 不推荐使用 environment.getexternalstoragedirectory() 后如何访问外部存储 - How to get access to external storage since environment.getexternalstoragedirectory() is deprecated getExternalStorageDirectory无效 - getExternalStorageDirectory not working getExternalStorageDirectory() 到 getExternalFilesDir() - getExternalStorageDirectory() to getExternalFilesDir() getExternalStorageDirectory() 已弃用 - getExternalStorageDirectory() is deprecated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM