How to delete cached image from android app - android

I am using square picasso library to download some images from one of our servers and load it in a list view. In my Android application I have a feature to change that downloaded image from app side and upload it to server.
I do know how to load the image from URL because it is well documented. What I need is to change/delete a particular cached item and replace it with my new image from Android application side.
Please help me.
Thanks in Advance....!

Suppose you have a List which stores your images, used by your ListView. You assign an Adapter
to your ListView which fills ListView from the List.
This is how you can replace an old (cached item) by the new one and release space for the old one:
public void replaceListItem (List<Bitmap> list, int position, Bitmap newBitmap) {
Bitmap oldBitmap = list.set(position, newBitmap);
if (oldBitmap != null)
oldBitmap.recycle(); // This will release space for the old bitmap
}
The function does not check if position is within the size, neither it checks if the new bitmap is already there, etc. I just wanted to concentrate on the question you asked.

Related

How to reduce the time for video thumbnail

So I am displaying video files from specific folder when the folder is selected but in the video list I am showing file name with the video thumbnail and that's where the problem starts i am using this way to get the thumb
Bitmap bmThumbnail = ThumbnailUtils.
extractThumbnail(ThumbnailUtils.createVideoThumbnail(info.getFilename(),
MediaStore.Video.Thumbnails.MINI_KIND), 60, 60);
if(bmThumbnail != null) {
imageView.setImageBitmap(bmThumbnail);}
and even if I don't set the bitmap or not the process is taking way too much time to open the new fragment whereas just displaying the name is smooth if I call the following bitmap it takes around 6-7 sec to display the list. The following thing are happening in the adapter as I test the app in adapter then recycler view
so I would like to know what is the best way to do it .
For using image loader i need the url which is not there as i am getting the album art using the file url but it will not produce the album art url directly.
Dealing with images and videos like that takes time. Particularly when dealing with a large group of them. It's unlikely you can speed up the operation but you can make it so your application doesn't have to wait for it by sending it to the background. I recommend Kotlin coroutines if you are up for converting to Kotlin. Otherwise I recommend making a thumbnail work manager
public class VideoThumbnailWorker extends Worker {
}

Android OutOfMemory

What is my Problem (KISS)?
After showing the 3rd Image in Detail my App crashes cause OutOfMemory
Background information about my App:
I made an offline Supermarket. All products are shown in a ListView.
All Images I use are stored as String (Uri from every Image) in a sqlite Database by SugarORM.
The ListView element got a ArrayAdapter where the Information for each Item in the ListView is set.
Like this:
ImageView imageView = (ImageView) rowView.findViewById(R.id.productimage);
Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
imageView.setImageURI(productList.get(position).getPictureUri());
handler.removeCallbacks(this);
}
});
My Intention was, to load the Images in the Background to Click on it, even before the Image is loaded (But it did'nt work for me :/ The Image is Shown, but I cant click on any other View Element while Loading the Image).
The "getPictureUri" gets the URI from the Picture that the Admin has choosen for the product with the following Code:
Intent pickPhoto = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto,1);
AndroidManifest.xml
android:hardwareAccelerated="false"
android:largeHeap="true"
I extend only from "AppCompatActivity" (Needed for Toolbar). Meaning of this: I don't use "Activity" even once.
Could anyone give me a good advice, how to clear the cached Images (I think this might be the Problem) and besides, maybe one of you got the Time to help me with my "Image Async Loading while you are able to move freely in your app" Problem.
Using setImageUri() is rather intensive on memory, especially when trying to load large bitmaps from a local resource. Instead, use tried and true solutions like Picasso or Glide for any/all image loading tasks. The libraries will work offline, can definitely load images from URIs pointing to a local resource and would most likely solve all your memory issues with image loading.

RxPparazzo retrieves the same images when I select more than one image

I coose RxPparazzo to manage the images in my app, but I'm still having the same problem. I don't know why when I select more than one image from gallery, the lib retrieves always the same image per image that it should have been added.
/**
* Pick some images from gallery using RxPaparazzo lib.
*/
private void takePhotosFromGallery() {
RxPaparazzo.takeImages(this)
.usingGallery()
.subscribe(response -> {
if (response.resultCode() != Activity.RESULT_OK) {
response.targetUI().showUserCanceled();
return;
}
response.targetUI().loadImages(response.data());
});
}
UPDATED
Fixed in version 0.1.3
The lib duplicate images ?
UPDATED
Here the explanation why RxPaparazzo create new images.
remove images after retrieving them
thanks for use RxPaparazzo
I've just released a new library version which hopefully fixes this issue.
I say 'hopefully' because I haven't been able to reproduce the bug, but looking at the file names, which matches, I think they were saved at the same second, so I've added the milliseconds to the end of the filename.
Try it out and let me know if it works for you.

Android: Universal image loader overrides old image

The user can upload an image and I want to display the image in a listview. This is the code so far:
final String urlForImage = baseUrlForImage + jsonObject.get("imageName");
new DownloadImageTask(cell.imageViewCustomer).execute(urlForImage);
final DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisc(true).resetViewBeforeLoading(true)
.showImageForEmptyUri(cell.imageViewCustomer.getDrawable())
.showImageOnFail(cell.imageViewCustomer.getDrawable())
.showImageOnLoading(cell.imageViewCustomer.getDrawable()).build();
imageLoader.displayImage(urlForImage, cell.imageViewCustomer, options);
But now if the user uploads a new image, the older one from the cache will be loaded in the listview and the new image will be loaded every time from the internet.
How can I override the old image from the user, if the user uploads a new image?
This could help you. Cleaning the cache (or a part of) sounds like a good way.
An easy solution is to change the url of the image, just add ?v=1 at the end and increment the number each time the user changes the image.

Use Bitmap Caching the correct way

Care: No code here, only text and some questions about bitmap caching
I'm currently developing an App which is almost finished. The only thing left, that I would like to do is caching images. Because, at the moment, when the user opens the app the app downloads images from a server. Those images are not static, that means they can change every minute/hour/day. I don't know when they change, because it's a list of images gathered by the amount of twitter shares, facebook likes etc. That means, when a picture has 100 likes and 100 tweets it is place 1. But when another picture gets more likes and tweets it gets rank 1 and the other one will be placed as rank 2. This isn't exactly my app, but just so you understand the principle.
Now I looked into Bitmap caching so the user doesn't have to download the same images over and over. The question I do have is how do I do it? I mean, i Understand HOW to cache bitmaps.
I looked into this documentation article: http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
But, the problem is, how do I know if the Bitmap already got downloaded and has been cached or if I have to download it again? Don't I have to download the image first to check if I have this particular image already in my system?
I thought about getting the URL of the image, then convert it into a hash. And then, save the files to the cache with the hash as filename. Then, when the image URL comes it will be checked wether the image is available in the cache or not. If it is it will be loaded if not it will be downloaded. Would that the way to go be?
Or am I misunderstanding bitmap caching and it does it from its own already?
my best advice on those cases is: Do not try to re-invent the wheel.
Image loading/caching is a very complex task in Android and a lot of good developers already did that. Just re-use their work.
My personal preference is Picasso http://square.github.io/picasso/
to load stuff with it is one very simple line of code:
Picasso.with(context).load(url).into(imgView);
it's that simple!
It does both RAM and disk cache, handles all threading issues and use the excellent network layer okHttp.
edit:
and to get access directly to the Bitmap you can:
Picasso.with(context).load(url).into(new Target() {
void onBitmapLoaded(Bitmap bitmap, LoadedFrom from){
// this will be called on the UI thread after load finishes
}
void onBitmapFailed(Drawable errorDrawable){
}
void onPrepareLoad(Drawable placeHolderDrawable){
}
});
Check this library:
http://code.google.com/p/android-query/wiki/ImageLoading
It does caching automagically
example
//fetch a remote resource in raw bitmap
String url = "http://www.vikispot.com/z/images/vikispot/android-w.png";
aq.ajax(url, Bitmap.class, new AjaxCallback<Bitmap>() {
#Override
public void callback(String url, Bitmap object, AjaxStatus status) {
}
});.
http://code.google.com/p/android-query/wiki/AsyncAPI
You can try https://github.com/thest1/LazyList
the project code was designed for listviews, but still, its purpose is to download images from URLs in the backgroud so the user doesn't have to hold on the whole downloading time.
you take these JAVA classes : FileCache, ImageLoader, MemoryCache, import them into your project,
for downloading an image you just call imageLoader.DisplayImage(URL,ImageView);
the best part is that it takes care of the cache itself so you don't have to worry about that
hope this helps

Categories

Resources