How can load images from an url with ion ? Image should be a bitmap.
Example: Bitmap image=Ion.with(this).load('url')
How can I achive this ?
Library:https://github.com/koush/ion
You should probably do it asynchronously. Here is an example:
Ion.with(this).load("url").withBitmap().asBitmap()
.setCallback(new FutureCallback<Bitmap>() {
#Override
public void onCompleted(Exception e, Bitmap result) {
// do something with your bitmap
}
});
It can also be loaded synchronously if needed. For example:
Bitmap image = Ion.with(this)
.load("url")
.withBitmap()
.asBitmap()
.get();
Related
I'm trying to load image stored on aws S3 into my android app using Picasso but I am getting a blank image with no errors in my logcat and nothing to me from general debugging around the relevant lines of code.
We are having private access on images so image url can't work on browser.
i need to display image into my android app using Picasso. but it doesn't work.
My code snippet below
new Picasso.Builder(getApplicationContext()).downloader(new S3Downloader(getApplicationContext(), s3Client, bucket))
.build()
.load("https://s3-ea-east-8.amazonaws.com/music/MusicApp_3.jpg")
.placeholder(R.drawable.img_placeholder)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.networkPolicy(NetworkPolicy.NO_CACHE)
.into(imageView);
By using above code image is displaying only very first time after installing app. next time its only showing placeholder image
I am using this library for displaying image.
The problem isn't with Picasso, it's with loading an image from a "private" url.
please suggest solutions
You need to generate a presigned Url from S3 client and you can pass that url to picasso.
That url will be public and will have an expriy date.
You can use the below set of code , which is a aynctask which will use glide to attach private images from s3 by generating presignedurl.
public class sets3imageasync extends AsyncTask<Object, Void, String> {
// The list of objects we find in the S3 bucket
static final String s3bucket=bucket_name;
Context scontext;
ImageView image;
#Override
protected String doInBackground(Object... objects) {
// Queries files in the bucket from S3.
Calendar cal = Calendar.getInstance();
Log.d(TAG, "doInBackground: in progress");
cal.setTime(new Date());
cal.add(Calendar.HOUR, +1);
Date oneHourLater = cal.getTime();
AmazonS3Client s3 = (AmazonS3Client) objects[0];
scontext= (Context) objects[1];
image=(ImageView) objects[2];
String imagekey=(String) objects[3];
GeneratePresignedUrlRequest generatePresignedUrlRequest =
new GeneratePresignedUrlRequest(s3bucket, imagekey)
.withMethod(HttpMethod.GET)
.withExpiration(oneHourLater);
URL url = s3.generatePresignedUrl(generatePresignedUrlRequest);
Log.e("url was", url.toString());
return url.toString();
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d(TAG, "onPostExecute: completed "+s);
Glide.with(scontext).load(s)
.apply(new RequestOptions()
.centerCrop())
.placeholder(scontext.getResources().getDrawable(R.drawable.progress_animation))
.into((image));
}
this can be called as below
new sets3imageasync().execute(s3Client,context,Image_view);
Simply use this:
Picasso.with(getApplicationContext()).load(your_url).noFade().into(imageView);
write below code to load image in Picasso.
variables:-
String file_path -->> this is your image file path
Imageview mViewHolder.img_post_photo -->> this is your imageview to load image.
Picasso.with(context)
.load(file_path)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.into(mViewHolder.img_post_photo, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Picasso.with(context)
.load(file_path)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.into(mViewHolder.img_post_photo);
}
});
Set dependencies in your app build.gradle file:-
compile 'com.squareup.picasso:picasso:2.5.2'
hope this code helps you.
Is there a way I can load images into Picasso's image cache by specifying the cache key that is used ?
On a side note if that is not possible, I have made the necessary changes but I am not sure how to rebuild the jar. Any instructions to rebuild Picasso are much appreciated.
You can call stableKey on your requestCreator object.
https://square.github.io/picasso/2.x/picasso/com/squareup/picasso/RequestCreator.html#stableKey-java.lang.String-
It will be like:
Picasso.with(context).load(yourURL).stableKey(yourKey).into(textView);
I not found no public access to this functionality, but for me works this workaround:
//1. Init picasso, create cache
mCache = new LruCache(mContext);
mPicasso = new Picasso.Builder(mContext).memoryCache(mCache).build();
//2. Load bitmap
mPicasso.load(uri).resize(mThumbWidth, mThumbHeight).centerCrop().into(v, callback);
//3. In case of error (for example, in callback), you can manually download the picture and store to cache
Bitmap bmp = <custom load>
//make key
StringBuilder sb = new StringBuilder(uri);
sb.append("\nresize:").append(mThumbWidth).append("x").append(mThumbHeight).append("\ncenterCrop\n");
mCache.set(sb.toString(), bmp);
Please note, that a key is used uri and your custom transformation (resize, centerCrop ant such, see more at com.squareup.picasso.Utils#createKey)
this worked for me
String strUrl = WWW.sultan.com....;
Bitmap = bitmap;
Picasso.with(Profile.this).load(strUrl).into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
userImage.setImageBitmap(bitmap);
this.bitmap = bitmap; //this can be used any where in the code
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
I'm trying to use the library volley, but I'm in doubt.
The volley library has some function that simply returns a bitmap?
pseudocode:
Bitmap bmp = Volley.getImageURL (url);
Thank you
I think it has such a functionality:
http://blog.lemberg.co.uk/volley-part-3-image-loader
Sample code:
String imageUrl = "http://some.server.com/image.png";
Volley.newRequestQueue(this).add(new ImageRequest(imageUrl, new Response.Listener<Bitmap>() {
#Override
public void onResponse(Bitmap bitmap) {
// Do something with loaded bitmap...
}
}, 1024, 1024, null, null));
But there is better library for loading images from the Internet - Picasso
Sample code:
String imageUrl = "http://some.server.com/image.png";
// This request is synchronous, so it shouldn't be made from main thread
Bitmap bitmap = Picasso.with(this).load(imageUrl).get();
I'm using ION (https://github.com/koush/ion) to load images from the web into a ListView of ImageView. Currently I want to get the bitmap from the ImageView, but I'm getting one exception that is force closing my app:
java.lang.ClassCastException: com.koushikdutta.ion.IonDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
These are the lines that I'm using:
final ImageView itemImageView = (ImageView)view.findViewById(R.id.photoImage);
final Bitmap itemDrawable = ((BitmapDrawable) itemImageView.getDrawable()).getBitmap();
How can I solve this? Thanks in advance!
Ion sets a custom drawable to manage animated GIFs, fade transitions, etc, so it can't be cast to a BitmapDrawable.
Use:
Ion.with(imageView).getBitmap()
to get the bitmap out
If you set a callback when making the Ion request and have it return an ImageViewBitmapInfo you can retrieve bitmaps that way. For e.g.
Ion.with(imageView)
.placeholder(R.drawable.default_avatar)
.load(userImageURL)
.withBitmapInfo()
.setCallback(new FutureCallback<ImageViewBitmapInfo>() {
#Override
public void onCompleted(Exception e, ImageViewBitmapInfo result) {
// Check for exceptions
// Check bitmaps first
Bitmap b = result.getBitmapInfo().bitmaps[0];
...
};
It may be worth looking into:
nostra13's "Universal Image Loader"
It's an awesome library with tons of features to display images from URLs, cast to bitmaps, etc.
if(imageView.getDrawable() instanceof BitmapDrawable)
bm = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
else { /***to check if it is ION drawable, use ION*/
bm = Ion.with(imageView).getBitmap();
}
I was using UniversalImageDownloader for my app.in UIL we can save images from cache memory.
File cachedImage = ImageLoader.getInstance().getDiscCache().get(imageUrl);
if (cachedImage.exists())
{// code for save 2 sd
}
Is it possible in picasso?
There is a private Method in Picasso -
Bitmap quickMemoryCacheCheck(String key) {
Bitmap cached = cache.get(key);
if (cached != null) {
stats.dispatchCacheHit();
} else {
stats.dispatchCacheMiss();
}
return cached;
}
Modify the source according to your need.
You can do like this , Use OkHttp & Picasso:
public class APP extends Application{
public static OkHttpDownloader okHttpDownloader;
#Override
public void onCreate() {
super.onCreate();
Picasso.Builder b = new Picasso.Builder(this);
okHttpDownloader = new OkHttpDownloader(this);
b.downloader(okHttpDownloader);
Picasso.setSingletonInstance(b.build());
}
}
Then get the File from OkHttp local cache:
Downloader.Response res = APP.okHttpDownloader.load(Uri.parse(your image Url),0);
Log.i(TAG,"Get From DISK: " + res.isCached() );
storeImageFile(res.getInputStream());
You can get bitmap from ImangeView onSuccess() callback
Picasso.with(context).load(path).into(imageView, new Callback(){
public void onSuccess() {
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
//save bitmap to sdcard
}
public void onError() {}
}
WARNING: Saved bitmap may be different to original bitmap.
Below code snippet will first make Picasso load image from cache, if failed then download and display image in imageView
You can debug memory performance with
Picasso.with(appContext).setIndicatorsEnabled(true);
green (memory, best performance)
blue (disk, good performance)
red (network, worst performance).
//Debugging memory performance https://futurestud.io/tutorials/picasso-cache-indicators-logging-stats
//TODO remove on deployment
Picasso.with(appContext).setIndicatorsEnabled(true);
//Try to load image from cache
Picasso.with(appContext)
.load(imageUrl)
.networkPolicy(NetworkPolicy.OFFLINE).placeholder(R.drawable.ic_launcher)
.placeholder(R.drawable.ic_launcher)
.resize(100, 100)
.error(R.drawable.ic_drawer)
.into(markerImageView, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
// Try online if cache failed
Picasso.with(appContext)
.load(imageUrl)
.placeholder(R.drawable.ic_launcher)
.resize(100, 100)
.error(R.drawable.ic_drawer)
.into(markerImageView);
}
});