freedom_wings4 2016-08-01 14:38 采纳率: 0%
浏览 1539

安卓,使用DiskLruCache的时候报错:edit didn't create file 1

以下是我的使用DiskLruCache的部分代码

 String str = Environment.getExternalStorageDirectory().getCanonicalPath() + "/2indextest";
File cachePath=new File(str);
if(!cachePath.exists()) cachePath.mkdirs();
myDiskLrucache=DiskLruCache.open(cachePath, 1, 2, 50 * 1024 * 1024);  //note that the 3rd param is 2 here

//get the data from internet,and write into the cache
public void writeToCache(final String uri,final String text)
{
    new Thread()
    {
        public void run()
        {
            try {
                String key = "test";
                editor = myDiskLrucache.edit(key);
                outputStream=editor.newOutputStream(0);
                if(download(uri, outputStream))            //the function download(...) is defined by myself,I will show it under this function
                    editor.commit();
                else editor.abort();
                index2=editor.newOutputStream(1);
                if(writeTextToStream(text,index2))          //这个writeTextToStream函数也是我自定义的,下面会贴出函数定义代码
                    editor.commit();                             //I think the error happens here,when writing the data with the index of 1 into the cache 
                else editor.abort();

                myDiskLrucache.flush();

                Message msg=myHandler.obtainMessage();
                msg.what=1;
                myHandler.sendMessage(msg);

            }
            catch(Exception e)
            {
                Log.e("this","some error happen when writing into the cache"+e);
            }
        }
    }.start();
}



private boolean download(String uri,OutputStream output)
{
    try {
        URL url = new URL(uri);
        HttpURLConnection con=(HttpURLConnection)url.openConnection();
        BufferedInputStream in=new BufferedInputStream(con.getInputStream());
        BufferedOutputStream out=new BufferedOutputStream(output);
        int b;
        while((b=in.read())!=-1)
            out.write(b);
        in.close();
        out.close();
    }
    catch(Exception e)
    {
        Log.e("this",e.toString());
    }
    return true;
}


private boolean writeTextToStream(String text,OutputStream out) throws Exception
    {

        ByteArrayInputStream inputStream=new ByteArrayInputStream(text.getBytes());
        int b;
        while((b=inputStream.read())!=-1)
            out.write(b);
        inputStream.close();
        out.close();
        return true;
    }


以上是部分代码,以下是logcat
图片说明

图片说明

  • 写回答

1条回答 默认 最新

  • devmiao 2016-08-02 05:56
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题