我有以下代碼:緩存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) {
}
但圖像沒有被加載。我可以改變什麼來完成這項工作?
謝謝你,它的工作原理,一個問題 - 會不會有螞蟻的方式來加速這一點?也許存儲uri而不是位圖? – Jack 2014-09-24 13:33:00