java.io.FileNotFoundException: xxx: open failed: EROFS (Read-only file system)

12-21 17:21:13.672: W/System.err(1583): java.io.FileNotFoundException: /downloadedMusic.mp3: open failed: EROFS (Read-only file system) 
12-21 17:21:13.672: W/System.err(1583): at libcore.io.IoBridge.open(IoBridge.java:416) 
12-21 17:21:13.672: W/System.err(1583): at java.io.FileOutputStream.<init>(FileOutputStream.java:88) 
12-21 17:21:13.672: W/System.err(1583): at java.io.FileOutputStream.<init>(FileOutputStream.java:73) 
12-21 17:21:13.672: W/System.err(1583): at crifan.com.crifanLib.downlodFile(crifanLib.java:197) 
12-21 17:21:13.672: W/System.err(1583): at crifan.com.crifanLibSongtaste.stDownloadFromUrl(crifanLibSongtaste.java:204) 
12-21 17:21:13.672: W/System.err(1583): at crifan.com.downloadsongtastemusic.MainActivity.preformDownload(MainActivity.java:74) 
12-21 17:21:13.672: W/System.err(1583): at java.lang.reflect.Method.invokeNative(Native Method) 
12-21 17:21:13.672: W/System.err(1583): at java.lang.reflect.Method.invoke(Method.java:511) 
12-21 17:21:13.672: W/System.err(1583): at android.view.View$1.onClick(View.java:3586)
12-21 17:21:13.672: W/System.err(1583): at android.view.View.performClick(View.java:4084) 
12-21 17:21:13.672: W/System.err(1583): at android.view.View$PerformClick.run(View.java:16966) 
12-21 17:21:13.672: W/System.err(1583): at android.os.Handler.handleCallback(Handler.java:615) 
12-21 17:21:13.672: W/System.err(1583): at android.os.Handler.dispatchMessage(Handler.java:92) 
12-21 17:21:13.672: W/System.err(1583): at android.os.Looper.loop(Looper.java:137) 
12-21 17:21:13.672: W/System.err(1583): at android.app.ActivityThread.main(ActivityThread.java:4745) 
12-21 17:21:13.672: W/System.err(1583): at java.lang.reflect.Method.invokeNative(Native Method) 
12-21 17:21:13.672: W/System.err(1583): at java.lang.reflect.Method.invoke(Method.java:511) 
12-21 17:21:13.672: W/System.err(1583): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
12-21 17:21:13.672: W/System.err(1583): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
12-21 17:21:13.672: W/System.err(1583): at dalvik.system.NativeStart.main(Native Method) 
12-21 17:21:13.672: W/System.err(1583): Caused by: libcore.io.ErrnoException: open failed: EROFS (Read-only file system) 
12-21 17:21:13.672: W/System.err(1583): at libcore.io.Posix.open(Native Method) 
12-21 17:21:13.689: W/System.err(1583): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) 
12-21 17:21:13.689: W/System.err(1583): at libcore.io.IoBridge.open(IoBridge.java:400) 
12-21 17:21:13.689: W/System.err(1583): … 19 more


很明显,是没有权限去访问对应的位置:

/downloadedMusic.mp3

所以,接着就是去,想办法,如何将下载的东西,存储到Android中的,某个位置,程序有权限访问的位置。

去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 java.io.File.createNewFile(File.java:940)

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

去改为:

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

而导致无法创建的。


本文出自 “没有水勒鱼” 博客,转载请与作者联系!

你可能感兴趣的:(android,System,failed)