最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】Android中的文件存储位置:java.io.FileNotFoundException: xxx: open failed: EROFS (Read-only file system)

Android crifan 6597浏览 0评论

【问题】

折腾:

【已解决】Android中利用HttpClient等库实现网络文件下载

的过程中,出错:

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中的,某个位置,程序有权限访问的位置。

 

【解决过程】

1.然后找到了官网的:

Using the External Storage

去看解释。

2.再参考:

External storage

的解释,去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

3.再参考:

Android数据存储之文件存储

先去改为:

		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)

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
57 queries in 0.086 seconds, using 18.84MB memory