。Environment.getExternalStorageDirectory()getAbsolutePath()给出了不同的道路 - 机器人 [英] Environment.getExternalStorageDirectory().getAbsolutePath() gives a different path - Android

查看:167
本文介绍了。Environment.getExternalStorageDirectory()getAbsolutePath()给出了不同的道路 - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写一些东西到我的手机内存。

起初,我硬codeD上的路径为:

  MYFILE =新的文件(/ SD卡/+ txtName.getText()+名.txt);
 

这工作完全正常。

然后,日食给了我一个警告,说我不应该硬codeD这样的路径,而不是,我应该做到以下几点:

  MYFILE =新的文件(Environment.getExternalStorageDirectory()getAbsolutePath()+/+ txtName.getText()+名.txt);
 

然后我跟着修正建议,并避免该警告。

不过,我在作家班遇到了运行时异常。

然后,我打印出 Environment.getExternalStorageDirectory()。getAbsolutePath()用于调试的目的。其结果是

  

/存储/ sdcard0

。但硬codeD一之前完全正常工作实际上是

  

/ SD卡

为什么会出现这种情况?

如果我想避免的警告,我怎么能得到硬编码路径更正规的和正确的方式的路径目录呢?

PS:我的手机是HTC One X的,它没有外置SD卡插槽。我猜32GB存储带有一个内置的SD卡,因此,在本质上应该是相同的。

解决方案
  

为什么会出现这种情况?

由于路径外部存储已经改变了多年来,这就是为什么你应该从来没有硬codeD摆在首位的路径。

  

我怎样才能硬编码路径更正规的和正确的方式的路径目录呢?

使用 Environment.getExternalStorageDirectory()

在您的测试环境, / SD卡是一个符号链接或硬连接到 /存储/ sdcard0 ,因为设置由设备制造商。然而,没有保证的所有的设备都会有这样的 / SD卡符号链接。

  

我想,32GB存储带有一个内置的SD卡

外部存储您的板载32GB闪存。一部分

I try to write something into my phone memory.

At first, I hard-coded the path as:

myFile = new File("/sdcard/" + txtName.getText() + ".txt");

This works totally ok.

And then, eclipse gives me a warning saying that I shouldn't have hard-coded the path like that instead, I should do the following:

myFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+ txtName.getText() + ".txt");

Then I follow the correction suggestion and avoid the warning.

However, I encountered a runtime exception on the writer class.

Then, I print out Environment.getExternalStorageDirectory().getAbsolutePath() for debugging purpose. The result is

/storage/sdcard0

. But the hardcoded one that worked perfectly fine before is actually

/sdcard

.

Why would this happen?

And if I wish to avoid the warning, how can I get the path directory in a more "formal and right" way instead of hardcoding the path?

P.S.: My phone is HTC One X, which has NO external SD card slot. I guess the 32GB storage comes with a built-in SD card, and therefore, the essence should be the same.

解决方案

Why would this happen?

Because the path to external storage has changed over the years, which is why you should have never hard-coded the path in the first place.

how can I get the path directory in a more "formal and right" way instead of hardcoding the path?

Use Environment.getExternalStorageDirectory().

On your test environment, /sdcard is a symlink or hardlink to /storage/sdcard0, as set up by the device manufacturer. However, there is no guarantee that all devices will have such an /sdcard symlink.

I guess the 32GB storage comes with a built-in SD card

External storage is a portion of your on-board 32GB of flash memory.

这篇关于。Environment.getExternalStorageDirectory()getAbsolutePath()给出了不同的道路 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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