Environment.getExternalStorageDirectory() on the emulator

拟墨画扇 提交于 2019-12-23 17:12:05

问题


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?


回答1:


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

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

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

EDIT: Also need to add

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

to the manifest.




回答2:


By default, the emulator will be launched without a sd card. You should create a sd card image first by using the mksdcard command in console. See SDK document:https://developer.android.com/studio/command-line/mksdcard.html

Hence, in the console, type the command to launch an emulator with that sd card:

    emulator -avd <emulator name> -sdcard <sdcard image name>

For better performance, I strongly suggest you debug your app via a real device.



来源:https://stackoverflow.com/questions/7591531/environment-getexternalstoragedirectory-on-the-emulator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!