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 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏