2014-09-24 39 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