【问题】
折腾:
【已解决】Android中利用HttpClient等库实现网络文件下载
的过程中,出错:
12-21 17:21:13.672: W/System.err(1583): java.io.FileNotFoundException: /downloadedMusic.mp3: open failed: EROFS (Read-only file system) |
很明显,是没有权限去访问对应的位置:
/downloadedMusic.mp3
所以,接着就是去,想办法,如何将下载的东西,存储到Android中的,某个位置,程序有权限访问的位置。
【解决过程】
1.然后找到了官网的:
去看解释。
2.再参考:
的解释,去AndroidManifest.xml中添加了:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
然后继续添加代码。
结果代码:
File extDir = Environment.getExternalStorageDirectory(); String filename = "downloadedMusic.mp3"; File fullFilename =new File(extDir.getAbsolutePath() + extDir.pathSeparatorChar + filename); try { fullFilename.createNewFile(); fullFilename.setWritable(Boolean.TRUE); songtaste.stDownloadFromUrl(strSongUrl, fullFilename); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
就出错了:
12-21 17:45:48.298: W/System.err(1547): java.io.IOException: open failed: EROFS (Read-only file system) 12-21 17:45:48.298: W/System.err(1547): at crifan.com.downloadsongtastemusic.MainActivity.preformDownload(MainActivity.java:83) 12-21 17:45:48.298: W/System.err(1547): at java.lang.reflect.Method.invokeNative(Native Method) 12-21 17:45:48.298: W/System.err(1547): at java.lang.reflect.Method.invoke(Method.java:511) 12-21 17:45:48.298: W/System.err(1547): at android.view.View$1.onClick(View.java:3586) 12-21 17:45:48.298: W/System.err(1547): at android.view.View.performClick(View.java:4084) 12-21 17:45:48.298: W/System.err(1547): at android.view.View$PerformClick.run(View.java:16966) 12-21 17:45:48.298: W/System.err(1547): at android.os.Handler.handleCallback(Handler.java:615) 12-21 17:45:48.298: W/System.err(1547): at android.os.Handler.dispatchMessage(Handler.java:92) 12-21 17:45:48.298: W/System.err(1547): at android.os.Looper.loop(Looper.java:137) 12-21 17:45:48.298: W/System.err(1547): at android.app.ActivityThread.main(ActivityThread.java:4745) 12-21 17:45:48.298: W/System.err(1547): at java.lang.reflect.Method.invokeNative(Native Method) 12-21 17:45:48.298: W/System.err(1547): at java.lang.reflect.Method.invoke(Method.java:511) 12-21 17:45:48.298: W/System.err(1547): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 12-21 17:45:48.298: W/System.err(1547): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 12-21 17:45:48.298: W/System.err(1547): at dalvik.system.NativeStart.main(Native Method) 12-21 17:45:48.298: W/System.err(1547): Caused by: libcore.io.ErrnoException: open failed: EROFS (Read-only file system) 12-21 17:45:48.309: W/System.err(1547): at libcore.io.Posix.open(Native Method) 12-21 17:45:48.309: W/System.err(1547): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) 12-21 17:45:48.309: W/System.err(1547): at java.io.File.createNewFile(File.java:933) 12-21 17:45:48.309: W/System.err(1547): … 15 more |
3.再参考:
先去改为:
File extDir = Environment.getExternalStorageDirectory(); String filename = "downloadedMusic.mp3"; File fullFilename = new File(extDir, filename); try { fullFilename.createNewFile(); fullFilename.setWritable(Boolean.TRUE); songtaste.stDownloadFromUrl(strSongUrl, fullFilename); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
结果是,可以正常新建文件:
/mnt/sdcard/downloadedMusic.mp3
看来之前就是由于:
/mnt/sdcard:downloadedMusic.mp3
而导致无法创建的。
4.有待后续。
转载请注明:在路上 » 【已解决】Android中的文件存储位置:java.io.FileNotFoundException: xxx: open failed: EROFS (Read-only file system)