Environment.getExternalStorageDirectory()上的模拟器模拟器、Environment、getExternalStorageDirectory

2023-09-07 13:32:12 作者:碎碎念

我想读取和写入图像到外部存储在我的应用程序。从我读了下面是正确的方式来获得对目录的句柄。

I want to read and write images to the external storage in my app. From what I read the following is the correct way to get a handle on the directory.

 File externalStorageDir = Environment.getExternalStorageDirectory();
 File picturesDir = new File(externalStorageDir, "Pictures");

不过...

However ...

 picturesDir.exists(); // == null
 picturesDir.mkdir(); // == false

这是因为我使用模拟器?

Is this because I'm using the emulator?

推荐答案

您可能希望确保您已外部存储启用:

You might want to make sure you have external storage enabled:

在Eclipse中,选择Window> Android SDK和AVD管理。选择适当的AVD,然后单击编辑。

In Eclipse, go to Window > Android SDK and AVD Manager. Select the appropriate AVD and then click Edit.

请确保您启用了SD卡的支持。如果不这样做,点击新建按钮,选择支持SD卡选项。

Make sure you have SD card support enabled. If you do not, click the "New" button and select the "SD Card Support" option.

编辑:还需要添加

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

到清单。