2014-09-24 72 views
0

我有以下代码:缓存exisiting文件

file = new File(getRealPathFromURI(uri)); 

我怎么能在高速缓存中存储这一个名字,所以我就可以访问它以后?

我知道有作为File outputDir = context.getCacheDir(); File outputFile = File.createTempFile("prefix", "extension", outputDir);

这种方法,但我不明白我怎么可以在缓存文件,以便然后在进一步的日期,我可以在其他activitys做file = new File(getActivity().getCacheDir(), "storedFileName");存储与一个特定的名称。

任何指导将是伟大的,谢谢。

编辑: 这是我的主要活动,其中我从画廊PIC,并返回作为onActivityResult一个URI:

@Override 
protected void onActivityResult(int requestCode, int resultCode, 
     Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch (requestCode) { 
    case SELECT_PHOTO: 
     if (resultCode == RESULT_OK) { 
      Uri selectedImage = imageReturnedIntent.getData(); 

      Intent i = new Intent(getApplicationContext(), 
        sliding_menu.class); 


      File file = new File(selectedImage.getPath()); 

       ObjectOutput out; 
       try { 
        String filenameOffer="Image"; 

        out = new ObjectOutputStream(new FileOutputStream(new File 
          (getCacheDir(),"")+filenameOffer)); 
        out.writeObject(file); 
        out.close(); 
       } catch (Exception e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
       } 
      startActivity(i); 

     } 
    } 
} 

正如你所看到的,我试图让所选图像的Uri,然后将其制作成文件。

然后我试图将文件存储在缓存中,以便我可以在整个应用程序中进一步检索它。

这里就是我试图访问该文件的下一个活动:

try { 
      String filename="Image"; 

      ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(new File(
        getActivity().getCacheDir(),"")+filename))); 
      String res = (String) in.readObject(); 



      Picasso.with(getActivity().getApplication()).load((res)) 
      .into(mImageView); 

     } catch (Exception e) { 


     } 

但图像没有被加载。我可以改变什么来完成这项工作?

回答

1

例与.png文件

保存文件:(InputStream的=来自互联网)

final Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 


    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.PNG, 80, bytes); 

    File f = new File(Environment.getExternalStorageDirectory() + File.separator + "MyApp/" + fileName); 

    if (!f.exists()) 
    { 
     f.getParentFile().mkdirs(); 
     f.createNewFile(); 
    } 

    FileOutputStream fo = new FileOutputStream(f); 
    fo.write(bytes.toByteArray()); 
    fo.close(); 

读取文件:

File path = new File(Environment.getExternalStorageDirectory(),"MyApp/" + fileName); 

     if(path.exists()) 
     { 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
      Bitmap bitmap = BitmapFactory.decodeFile(path.getAbsolutePath(), options); 
     } 
+0

谢谢你,它的工作原理,一个问题 - 会不会有蚂蚁的方式来加速这一点?也许存储uri而不是位图? – Jack 2014-09-24 13:33:00

0

要存储在缓存文件,你可以使用你的数据,

假设响应是要存储在缓存中的字符串。

  ObjectOutput out; 
      try { 
       String filenameOffer="cacheFileSearch.srl"; 

       out = new ObjectOutputStream(new FileOutputStream(new File 
         (getActivity().getCacheDir(),"")+filenameOffer)); 
       out.writeObject(response); 
       out.close(); 
      } catch (Exception e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 

并获取数据从缓存文件回来,

try { 
     String filename="cacheFileSearch.srl"; 

     ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(new File(
       getActivity().getCacheDir(),"")+filename))); 
     String res = (String) in.readObject(); 

    } catch (Exception e) { 
     AppConstant.isLoadFirstTime=true; 
    } 

而对于删除的文件,你可以使用

 String filename="cacheFileSearch.srl"; 

     try { 
      ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(new File(
        getActivity().getCacheDir(),"")+filename))); 
      File dir = getActivity().getCacheDir(); 
      if (dir.isDirectory()) { 
       if (new File(new File(dir, "")+filename).delete()) { 
       } 

      } 
      in.close(); 

     } catch (Exception e) { 

     } 
+0

您提供的代码在我的实例中不起作用。我已经更新了我想要做的事情,谢谢。 – Jack 2014-09-24 12:58:56