Reputation: 2053
I have the following code:
file = new File(getRealPathFromURI(uri));
How am I able to store this in the cache with a name so I can then access it later on?
I know there are methods such as File outputDir = context.getCacheDir();
File outputFile = File.createTempFile("prefix", "extension", outputDir);
But I don't understand how I can store this file in the cache with a specific name so then at a further date I can do file = new File(getActivity().getCacheDir(), "storedFileName");
in other activitys.
Any guidance would be great, thanks.
EDIT: Here is my main activity where I get a pic from the gallery and it is returned as a uri in the onActivityResult:
@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);
}
}
}
As you can see, I am trying to make the Uri of the selected image, then make it into a file.
Then I am trying to store the file in the cache so I can then further retrieve it throughout my application.
Here is the next activity where I am trying to access the file:
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) {
}
But the image isn't loading. What can I change to make this work?
Upvotes: 0
Views: 190
Reputation: 2045
Example with a .png file
Save File:( InputStream = from internet )
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();
Read File:
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);
}
Upvotes: 1
Reputation: 2354
To store your data on cache file you can use,
Suppose response is string which you want to store in cache.
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();
}
And for getting data back from cache file,
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;
}
And for deleting file, you can use
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) {
}
Upvotes: 0