java - 使用retrofit2下載歌詞檔案後,進行儲存時報空間不足的錯誤
大家讲道理
大家讲道理 2017-05-24 11:38:18
0
1
1088

問題:
寫入一個不到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.handhandleback(Handler.dler. )
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#icky I/at: 007470 0 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;
}
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回覆(1)
为情所困

while (fileread!= -1){

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

這個使用循環是不對的,這塊應該會將循環改成if

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板