我有以下代码:缓存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