使用retrofit2下载歌词文件后,进行保存时报空间不足的错误

问题:
写入一个不到2kb的文件时直接报空间不足的问题,使用的是retrofit2请求的文件,哪位大神有时间给看看是什么原因吧

报错的详情:
D/OkHttp: --> GET http://musicdata.baidu.com/da... http/1.1
D/OkHttp: <-- 200 OK http://musicdata.baidu.com/da... (98ms, 1877-byte body)
D/ContentValues: onResponse: 歌词文件请求成功
I/art: Thread[2,tid=3036,WaitingInMainSignalCatcherLoop,Thread*=0xae402000,peer=0x12c930a0,"Signal Catcher"]: reacting to signal 3
I/art: Wrote stack traces to '/data/anr/traces.txt'
W/System.err: java.io.IOException: write failed: ENOSPC (No space left on device)
W/System.err: at libcore.io.IoBridge.write(IoBridge.java:498)
W/System.err: at java.io.FileOutputStream.write(FileOutputStream.java:186)
W/System.err: at com.wang.mymusic.data.MusicData.lrcFile(MusicData.java:163)
W/System.err: at com.wang.mymusic.data.MusicData.access$200(MusicData.java:40)
W/System.err: at com.wang.mymusic.data.MusicData$3.onResponse(MusicData.java:123)
W/System.err: at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:148)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
W/System.err: Caused by: android.system.ErrnoException: write failed: ENOSPC (No space left on device)
W/System.err: at libcore.io.Posix.writeBytes(Native Method)
W/System.err: at libcore.io.Posix.write(Posix.java:271)
W/System.err: at libcore.io.BlockGuardOs.write(BlockGuardOs.java:313)
W/System.err: at libcore.io.IoBridge.write(IoBridge.java:493)
W/System.err: ... 12 more
I/art: Background partial concurrent mark sweep GC freed 83995(3MB) AllocSpace objects, 0(0B) LOS objects, 12% free, 27MB/31MB, paused 3.141ms total 131.690ms
I/art: Background sticky concurrent mark sweep GC freed 104777(4MB) AllocSpace objects, 0(0B) LOS objects, 0% free, 34MB/34MB, paused 5.311ms total 163.136ms

代码:

//保存歌词文件到磁盘

private File lrcFile(Context context,ResponseBody respon,String title,String author) throws IOException {
    //create a file
    File filedirectory;
    File file;
    String name=title+"_"+author+".lrc";
    if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()){
        filedirectory=new File(context.getExternalCacheDir()+File.separator+"lrc");
        file=new File(filedirectory+File.separator+name);
    }else {
        filedirectory=new File(context.getCacheDir()+File.separator+"lrc");
        file=new File(filedirectory+File.separator+name);
    }
    if (!filedirectory.exists()){
        filedirectory.mkdir();
    }
    if(!file.exists()){
        file.createNewFile();
    }
    InputStream in=respon.byteStream();
    try {
        OutputStream out=new FileOutputStream(file);
        byte[] b=new byte[2048];
        int fileread=in.read(b);
        while (fileread!= -1){
            out.write(b,0,fileread);
        }
        out.flush();
        out.close();
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return file;
}
阅读 5.2k
4 个回答

while (fileread!= -1){

        out.write(b,0,fileread);
    }

这个使用循环是不对的,这块应该将循环改成if

代码问题基本上是while大部分情况死循环没跑了,先试试改成if判定,然后看看是不是还报内存不足。

代码问题基本上是while大部分情况死循环没跑了,先试试改成if判定,然后看看是不是还报内存不足。

你代码里的循环出现了问题,

while (fileread!= -1){
   out.write(b,0,fileread);
 }
 

中fileread每次都是固定值,肯定是不等于-1的,所以是死循环,应该修改为:

  while ((fileread=in.read(b))!= -1){
        out.write(b,0,fileread);
    }
    

这样,fileread是变化的了,可以用来判定是否到末尾。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏