开发者

Environment.getExternalStorageDirectory() on the emulator

I want to read and write images to the external storage in my app. From what I read the foll开发者_运维百科owing 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:

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

Environment.getExternalStorageDirectory() on the emulator

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

Environment.getExternalStorageDirectory() on the emulator

EDIT: Also need to add

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

to the manifest.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜