我有以下代码:
file = new File(getRealPathFromURI(uri));
我如何能够使用名称将其存储在缓存中,以便稍后可以访问它?
我知道有File outputDir = context.getCacheDir();
File outputFile = File.createTempFile("prefix", "extension", outputDir);
但我不明白如何将此文件存储在具有特定名称的缓存中,以便在其他日期我可以file = new File(getActivity().getCacheDir(), "storedFileName");
执行其他活动。
任何指导都会很棒,谢谢。
修改 这是我的主要活动,我从画廊获得一张照片,并在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) {
}
但是图片没有加载。我可以改变什么来使这项工作?
答案 0 :(得分: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);
}
答案 1 :(得分: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) {
}