writing to sdcard not working

不问归期 提交于 2019-12-25 11:57:10

问题


I am running Android in Eclipse. the following line (from Camera.demo)

outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", 
    System.currentTimeMillis()));

fails and returns the following error:-

FileNotFound - permission denied.

Is there anything I can do to correct this? I have assumed that the sdcard directory is present in the emulator. Thanks Ron


回答1:


The AVD you're running has to be explicitly configured with SD card support or it will behave as if there isn't one. More information here.

Your application also needs the WRITE_EXTERNAL_STORAGE permission.

Side note: You should be calling Environment.getExternalStorageDirectory() to get the SD card's path instead of hard-coding /sdcard. It returns null if there's no storage available. You can use the canWrite() method on the returned value to determine if it's actually something you can use for storage.



来源:https://stackoverflow.com/questions/3741701/writing-to-sdcard-not-working

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