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 morejava


很明顯,是沒有權限去訪問對應的位置:android

/downloadedMusic.mp3app

因此,接着就是去,想辦法,如何將下載的東西,存儲到Android中的,某個位置,程序有權限訪問的位置。ide

去AndroidManifest.xml中添加了:oop

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

而後繼續添加代碼。spa

結果代碼:orm

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();
}

就出錯了:xml


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)ci

12-21 17:45:48.298: W/System.err(1547):     at crifan.com.downloadsongtastemusic.MainActivity.preformDownload(MainActivity.java:83)get

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

而致使沒法建立的。

相關文章
相關標籤/搜索