I've tried lots of different ways to download images and none of them actually worked, i have managed to get something working but its not perfect.
I made a new thread for downloading the bitmaps, im not storing them on the system storage or caching them in the memory for later use. If i keep opening this activity over and over, the ram usage for this app keeps getting higher and higher, and i do not want that at all!
URL url = new URL("http://10.0.0.21:80/1.png");
bitmapOne = BitmapFactory.decodeStream(url.openStream());
URL url2 = new URL("http://10.0.0.21:80/2.png");
bitmapTwo = BitmapFactory.decodeStream(url2.openStream());
runOnUiThread(new Runnable() {
#Override
public void run() {
image.setImageBitmap(bitmapOne);
image2.setImageBitmap(bitmapTwo);
}});
Thank you all for your answers! but...
I would only like to use pure java and no third party libraries for loading images, I was looking for a way to reduce memory usage and not so much of finding another way of downloading them. Im also not sure that having two urls and loading each stream looks very professional as the client could disconnect at any given moment (lets just pretend it does) and it does not load them both which are extremely required!
Use Picasso library, everything will be a breeze:
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Picasso.with(this)
.load("http://SomeUrl/CodeFeature.jpg")
.into(imageView);
A good tutorial can be found here.
https://github.com/zetbaitsu/Compressor
Usage:
compressedImageBitmap = Compressor.getDefault(this).compressToBitmap(actualImageFile);
This library maybe helpful for you.
You can visit GitHub. search "Android", sort by: "Most Stars"
, lots of awesome library you will found.
Volley can be a good alternative too, with the ability of displaying animated gifs. This snippet illustrate the basic use, here with image cache, something that can make your app not to be memory and network too demanding:
ImageLoader.ImageCache imageCache = new BitmapLruCache();
ImageLoader imageLoader = new ImageLoader(Volley.newRequestQueue(getApplicationContext()), imageCache);
NetworkImageView myImage = (NetworkImageView) findViewById(R.id.myImageView);
myImage.setImageUrl(""+mSpeaker.getHeadshotUrl(), imageLoader);
loadBitmap("http://yourdomain.com/yourImage.jpg", myImage);
For this to work all you need is the Volley library and BitmapLruCache.java
Related
I'm using Fresco library to display images in my Android app. I'd like to display some images (jpg or png) that I have set with public grants.
When I was doing quick tests, I just took any image from internet to set a URL, but when using the real ones that I need to use, I have the following url https://drive.google.com/uc?export=view&id=<>, but as it is a redirect and, once redirected, new url is not the image itself, Fresco is unable to display it.
I have tried Picasso as an alternative library, but with out any success.
I have also tried the download url for both libraries (https://drive.google.com/uc?export=download&id=<>). But no result.
Anybody knows how could it be possible to get this images? Or the only solution is to download it (using the second url) processing the object received store a bitmap of it and displaying it?
For downloading it, what should i use and how? retrofit?
Thanks in advance.
Fresco supports different network stacks. For example, you can use OkHttp with Fresco, which should follow redirects or modify the default one to allow redirects - or write your own based on them.
Guide for OkHttp: http://frescolib.org/docs/using-other-network-layers.html
Related GitHub issue: https://github.com/facebook/fresco/issues/61
I found a solution for this problem (but could be only applicable if you use Google Cloud or Google Script).
It consists on creating a doGet() service with the following code inside:
var file = DriveApp.getFileById(fileId)
return Utilities.base64Encode(file.getBlob().getBytes());
and use that base64 value in your app. With this format, Fresco can do the magic
It is not an immediate solution, and requires to do somework in other platform that is not your Android app, but it works perfectly.
Are you sure that there is no problems with your URLs?
Picasso works with direct URLs like: https://kudago.com/media/images/place/06/66/06662fda6309ce1ee9116d13bd1c66d5.jpg
Then you can download your image like:
Picasso.with(this)
.load(url)
.noFade()
.placeholder(R.drawable.placeholder_grey) //if you want to use a stub
.into(imageView, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
//here you can operate with image after it is downloaded
}
#Override
public void onError() {
}
});
Hope it will help you.
I am using Picasso to handle image loading and caching in my Android Udacity project and I am noticing the caching is not working as I'd expect:
As you can see on the left fragment, the image has already loaded in an earlier thread. Now, with the same URL link, I am asking Picasso to place that image in the fragment on the right.
Here is the code which generates the grid view on the left fragment (and occurs first):
https://github.com/esend7881/udacity-android-popmovie/blob/a9a1b9a19a37594bb5edd736b7ec59229fb5905a/app/src/main/java/com/ericsender/android_nanodegree/popmovie/adapters/GridViewAdapter.java#L71
String load = String.format(sImgUrl, sImgSize, movie.poster_path);
Picasso.with(mContext.getApplicationContext())
.load(load)
.placeholder(R.drawable.abc_btn_rating_star_on_mtrl_alpha)
.error(R.drawable.abc_btn_rating_star_off_mtrl_alpha)
.resize(550, 775)
.into(viewHolder.imageView);
And then here is the code which runs in the right fragment:
https://github.com/esend7881/udacity-android-popmovie/blob/a9a1b9a19a37594bb5edd736b7ec59229fb5905a/app/src/main/java/com/ericsender/android_nanodegree/popmovie/fragments/MovieDetailsFragment.java#L308
Picasso.with(getActivity().getApplicationContext())
.load(String.format(sImgUrl, sImgSize, mMovieObj.poster_path))
.error(R.drawable.blank)
.fit()// .resize(366, 516)
.into(mMovieThumb, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
Utils.log(sw.toString());
Utils.hideViewSafe(mMovieThumbProgress);
}
#Override
public void onError() {
Utils.log(sw.toString());
Utils.hideViewSafe(mMovieThumbProgress);
}
});
I am using the same application context in each as well as the load text:
String.format(sImgUrl, sImgSize, mMovieObj.poster_path))
and
getActivity().getApplicationContext()
So, I would think Picasso ought to detect when the exact same URL load link appears in the same context within a short period of time from each other and Picasso would then load the exact same image back into the app.
If this is not how Picasso caching works, then how does it?
As a comment mentioned, I'd guess this is affected by the size of the image being different in both fragments.
I'd recommend using https://github.com/facebook/fresco instead of picasso. It's more efficient, especially with different sizes. You can also directly access cached files if required https://github.com/facebook/fresco/issues/80
It's probably related to the HTTP headers received when getting the image that do not allow caching, as Picasso relies on an HTTP component to do the caching.
Try uploading your image on imgur, try hardcoding that path and see if it works. If that's the case, you'll have to find a workaround on how to get the image from the movie database.
I'm using Picasso Library and i have (for example) five ImageView and i need to show them with Picassoand i know we can do like this:
ImageView footer1 = (ImageView) findViewById(R.id.img_thumbnail1);
Picasso.with(MainActivity.this).load("http://url.com/1.jpg").into(footer);
ImageView footer2 = (ImageView) findViewById(R.id.img_thumbnail2);
Picasso.with(MainActivity.this).load("http://url.com/2.jpg").into(footer);
ImageView footer3 = (ImageView) findViewById(R.id.img_thumbnail3);
Picasso.with(MainActivity.this).load("http://url.com/3.jpg").into(footer);
ImageView footer4 = (ImageView) findViewById(R.id.img_thumbnail4);
Picasso.with(MainActivity.this).load("http://url.com/4.jpg").into(footer);
ImageView footer5 = (ImageView) findViewById(R.id.img_thumbnail5);
Picasso.with(MainActivity.this).load("http://url.com/5.jpg").into(footer);
But, can we use AsyncTask for load these multiloading?
Or what is the best way for do?
Picasso in the above case is a singleton instance, meaning you aren't creating a new Picasso object each time you load an image. Also the Images are loaded intelligently on a background queue with a designated task dispatcher so doing what you are doing above is perfectly fine. Do it in a loop if you need to cut the code.
You can of course use AsyncTask, but it will not make it any faster. In contrary it will bring more code complexity and overhead.
Picasso will also automatically manage the number of images it tries to download at once depending on device connection type (3G, Wifi etc).
I have a ListView with ~100 large images. Each image is about 640.640 jpg and is shown 1/2 screen size. Typical gallery.
I am currently loading the images using the wonderful universal-image-loader-1.9.2.
ParseFile photoFile = p.getParseFile("imageFile");
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage( photoFile.getUrl(), theImageView );
Now, you get a bit of stutter on the ListView, when they are loading.
Now, coincidentally, I had the phone on a very slow WiFi network. IN FACT, IT WAS SMOOTH AS SILK.
At first, I couldn't work out why the sudden improvement! Serendipity!
In fact then, in that situation: is it worth staggering the calls to load the images?
So, I could leave 0.1" between each call, for example.
Or indeed, should I just do them one follows the other maybe?!
What's the deal there? Do all android engineers do this all the time? Cheers!
I've already applied all the other classical techniques I have googled up for buttery smooth ListView scrolling. Cheers
(Note that I have tried a number of systems to load the images, doesn't make a difference, it's not an issue with Universal Image Loader, which rocks.)
ashishduh, here's the code from getView ... as you see, it's trivial.
(note - setting the size makes no difference. if i try it with say 500.500 fixed, it's the same)
...
v.someTextView.setText(blah);
v.someTextView.setText(blah);
// and now the big image
if (p.containsKey("imageFile"))
{
// this cell has a big image
v.mainImage.setVisibility(View.VISIBLE);
ParseFile photoFile = p.getParseFile("imageFile");
// ...using p.getNumber("imageWidth").floatValue(); and height
// from the cloud, calculate needed width/height on the phone...
v.mainImage.getLayoutParams().width = Math.round(desiredGlassWidth);
v.mainImage.getLayoutParams().height = Math.round(desiredGlassHeight);
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage( photoFile.getUrl(), v.mainImage );
}
else
{
// this pcell has no big image
v.mainImageIV.setVisibility(View.GONE);
}
...
For the record for anyone googling here ...
I just tried Picasso ... per the suggestion in the comment (thanks)
Really, it's fantastic. Only Volley would be better.
Picasso works so well, my question about staggering image calls is pointless.
http://square.github.io/picasso/
Hope it helps someone!
Any better answers, post 'em here for points!
PS1 an important point is that Picasso has a "fetch" command, which is a pre-warm. You can use this for even greater results, in some case.
PS2 as I mention above, the plain fact is the only thing better than Picasso, is moving to Volley. Hope it helps someone.
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