Store image key hashmap (persistent data) in internal/external storage - android

I am building a multilevel navigation drawer in my application where I would need to cache some 70-80 images which I do not want to download in front of users. Right now, I am using picasso image library which allows me to load images once the app gets started.
Load:
Picasso.with(getActionBar().getThemedContext()).load(imageURL);
Setting image in view:
Picasso.with(_context).load(imageURL).into(ImageView);
I want to store the images in user's phone so that the drawer could show them up even when the user is offline. I am not sure if I should use internal or external storage(also how would I use). Is there a way I could store these images in phone using picasso somehow because the effects picasso has for image loading seems good.
If not, how should I go about storing my images? I cannot hardcode this because these images would change every 2-4 days.

You can save the images like this:
File sdcard = Environment.getExternalStorageDirectory();
File f = new File (sdcard, "filename.png");
FileOutputStream out = new FileOutputStream(f);
yourBitmap.compress(Bitmap.CompressFormat.PNG, 90, out)

Related

Android create file without saving

File tempFile = File.createTempFile(""+Common.getMobileNumber(), ".jpg", null);
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(bos.toByteArray());
I do this, but thing is ...when i go to apps, and click on my app... i see that its adding caching. and this particular file is caching, how can i create file without caching/saving.
only need of file for me, is to send it over to server side. Is there any way i create a object file without saving/caching?
If you are saying you need like a file only in the memory for the particular task like uploading to server. It is very similar to this question to do that. There is a code to create a file in memory.
Android Creating a memory resident input file that can be attached to an email

How can force to add an image to the disk cache

I'm having troubles adding a local image to the disk cache manually. The reason is that I have a local photo and I need to upload this. But since I know the URL result, I want to cache this immediately. I'm using
ImageLoader.getInstance().getDiscCache().put(imageUrl, new File(tempFileURI));
But this doesn't work because when I use the line below, the file returns null
File thumb=
DiscCacheUtil.findInCache(urlThumbSize,ImageLoader.getInstance().getDiscCache());
Any idea?
Actually DiscCache is not intended to be used directly. DiscCache.put() doesn't copy your file into disk cache. It just control cache size if it's limited cache.
You should copy image file into cache directory yourself:
File fileInDiscCache = discCache.get(imageUri);
// copy your file into 'fileInDiscCache' file
discCache.put(imageUri, fileInDiscCache);

Android need help to sort saving images from web

I have a problem when saving image from web.
I create new folder and save all images to this. The problem is I want this folder order by the last download image on first but When i open gallery, the folder is order by the image date created and the date created in the downloaded image is not the time I download but the first time it created. I've already search on stack over flow and see that java can't modified the date created in image to the time I download it.
Any one has the solution? (Sorry for the bad English)
Thanks you for the comment. I will explain more details.
First, I download image from web to cache directory
HttpURLConnection localHttpURLConnection = (HttpURLConnection) new java.net.URL(urldisplay).openConnection();
localHttpURLConnection.setConnectTimeout(30000);
localHttpURLConnection.setReadTimeout(30000);
localHttpURLConnection.setInstanceFollowRedirects(true);
InputStream in = localHttpURLConnection.getInputStream();
File localFile = Constans.fileCache.getCacheFile(urldisplay);
FileOutputStream fos = new FileOutputStream(localFile);
Utils.CopyStream(in, fos); // simple copy by trunks
fos.close();
Second, I copy downloaded image to external storage
File toFile = new File(Environment.getExternalStorageDirectory() + "/folder", "folder_" + System.currentTimeMillis() + ".png");
FileOutputStream fos = new FileOutputStream(toFile);
Utils.CopyStream(new FileInputStream(fromFile), fos);
fos.close();
// Scan image to display when open with gallery otherwise it couldn't see in gallery
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(toFie);
mediaScanIntent.setData(contentUri);
mContext.sendBroadcast(mediaScanIntent);
Lastly, I see that gallery don't sort my image by the time I downloaded. That is the problem i want to fix.
Not sure I understood, but let's try.
First the issue you mention is more specific to Gallery application than a java code issue.
I assume Gallery use EXIF information to order the picture by date they were taken, not oder they are downloaded/copied. Unfortunatly Gallery does not provide any option to sort picture in other oders.
Maybe you can try to use another explorer that allows you to sort the pictures in another order (maybe ESFileExplore which has more options?)
Ultimate solution: you can try to change EXIF in your pictures using a java EXIF library to modify picture taken date and this should change the order they appear in the Galery (but very ugly solution...). Some random EXIF libraries after 5 seconds of Google:
http://drewnoakes.com/code/exif/
http://www.toanthang.net/modules.php?name=News&new_topic=2&catid=7
Hope this helps
Thierry

Android: Saving a created bitmap to some equivalent of SharedPreferences?

So I'm having the user create a profile, which includes an avatar. I would like the program to, like the user's name and other info, remember the avatar so it appears as their avatar whenever they use the app. I'd prefer for this bitmap to be saved when created, so the app doesn't need to rebuild the avatar (which is scaled and whatnot) each time the app is started. Is this possible? From the looks of it this wouldn't be able to be saved in SharedPrefs... any idea what the best way to do this would be? Thanks.
Followup: Followed CommonWares's suggestion. Saved to SD... but having trouble calling it back when the app is reloaded.
Currently, in onCreate:
String path = "testapp/images/thumbs/"+m_username+".jpg";
File file = new File("testapp/images/thumbs/"+m_username+".jpg");
if(file.exists()) {
Log.e("TRY TO GET THUMB", "I EXIST");
m_thumb = BitmapFactory.decodeFile(path);
Drawable draw = new BitmapDrawable(getResources(), m_thumb);
m_photoButtonE.setBackgroundDrawable(draw);
}
Does not find the file, says it does not exist, though when I check my SD card the image is in that exact spot, with the proper file name and everything. Any ideas? Thanks all.
Is this possible?
Save it in a file.
From the looks of it this wouldn't be able to be saved in SharedPrefs... any idea what the best way to do this would be?
Save it in a file. If you intend on having multiple avatar files, put a path to the saved avatar file in the SharedPreferences.

Android: getting thumbnails from specific location on sd card

AFAIK accessing thumbnails for images via MediaStore.Images.Thumbnails would generate thumbnails at first attempt, and that's what I need to perform against specific location on sd card.
The question is how to make valid URI to content under specific folder?
All answers I can find use just MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI as uri to make managedQuery. And result of it is Cursor that points to all sdcard images, while none examples can be found on how to access only specific folder.
Maybe you could just list files in the directory and parse them to get thumbnails without using content provider. You can use inSampleSize option to get small bitmap not the complete image Strange out of memory issue while loading an image to a Bitmap object.
may be is to late, but for some one will be helpfull
Mihai Fonoage said...
Use something like
File imagesDir = new File(Environment.getExternalStorageDirectory().toString() + "/pathToDirectory");
File[] imageList = imagesDir.listFiles();
for (File imagePath : imageList) {
bitmap = BitmapFactory.decodeStream(imagePath.toURL().openStream());}
Here you have some great tutorial.

Categories

Resources