Am trying to load a file as bitmap into TouchImageview. If I use normal image view instead of Touch image view Glide library is able to load image into it from file object but in case on touch image view Glide unable to load image.
Used following code as well:
Glide.with(this).asBitmap().load(file).into(new SimpleTarget<Bitmap>(250, 250) {
#Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
touchImageView.setImageBitmap(resource);
}
#Override
public void onLoadFailed(#Nullable Drawable errorDrawable) {
super.onLoadFailed(errorDrawable);
}
});
But OnLoadFailed() is called with errorDrawable as null.
Ok I just found about xml. please take note that, you HAVE TO USE RELATIVE LAYOUT!
<RelativeLayout
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">
<my.zuhrain.kit.TouchImageView
android:layout_marginTop="20dp"
android:id="#+id/image_view_main"
android:layout_width="400dp"
android:layout_height="400dp" />
</RelativeLayout>
and here we go
storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
//things need to know
//USE RELATIVE LAYOUT!
Glide.with(getApplicationContext())
.asBitmap()
.load(uri)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
touchImageView.setImageBitmap(resource);
}
});
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
});
Related
I need to load image using Picasso/Glide into View, not ImageView.But there isn't such method. Are there any probabilities?
Thanks everyone for answers!
With Picasso, you can load into a Target, like this:
Picasso.with(context).load(xyz).into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
// Load the bitmap into your view
}
#Override
public void onBitmapFailed() {
}
});
Then you can do with the bitmap whatever you want, like loading it into a custom view.
If you want don't want Glide to insert the image directly into an ImageView, then you can always just load the image and do what you want with it, for example:
Glide.with(getContext())
.load(uri)
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(final Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
// Do anything with the new Bitmap resource you have
}
});
I have used Picasso to load an image from my company's CDN into a ImageView:
ImageView imgView;
//...
Picasso.with(context).load(Uri.parse(url)).into(imgView);
But now I need load an image as a layout background:
RelativeLayout theLayout;
//...
Picasso.with(context).load(Uri.parse(url)).into(...);
Is it possible with Picasso? If not, should I use a ImageView instead of Relativelayout?
you can use glide to download bitmap and set it as background from any layout.
Glide
.with(getApplicationContext())
.load("https://www.google.es/images/srpr/logo11w.png")
.asBitmap()
.into(new SimpleTarget<Bitmap>(100,100) {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
Drawable dr = new BitmapDrawable(resource);
theLayout.setBackgroundDrawable(dr);
// Possibly runOnUiThread()
}
});
but the better way is to use imageView on top of relativelayout and make it match_parent and show image on this imageview. this will help you directly use glide or picaso to load image in image view without memory errors.
Picasso.with(getActivity()).load(your url).into(new Target(){
#Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
yourlayout.setBackground(new BitmapDrawable(context.getResources(), bitmap));
}
edit:
you may need to override following methods as well
#Override
public void onBitmapFailed(final Drawable errorDrawable) {
Log.e("TAG", "Failed");
}
#Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Log.e("TAG", "Prepare Load");
}
}
Yes. You can use Picasso for this. Please check following code :
Picasso.with(getActivity()).load(Uri.parse(url)).into(new Target(){
#Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
relativeLayout.setBackground(new BitmapDrawable(context.getResources(), bitmap));
}
#Override
public void onBitmapFailed(final Drawable errorDrawable) {
Log.d("TAG", "FAILED");
}
#Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Log.d("TAG", "Prepare Load");
}
})
This easy to achieve using Picasso
Picasso.with(holder.mImageView.getContext())
.load(item.getUrl())
.into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
holder.mImageView.setImageBitmap(bitmap);
holder.mLoadingImageView.setVisibility(View.GONE);
holder.updatePalette();//the logic of generate diffrent background colors
Log.d(TAG, "on success");
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
holder.mLoadingImageView.setVisibility(View.GONE);
Log.d(TAG, "on error");
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
holder.mLoadingImageView.setVisibility(View.VISIBLE);
}
});
and the holder take care of that logic (getting different colors for unloaded image) from updatePalette function, here its code or the whole demo if you want
in glide what?
Glide.with(holder.mImageView.getContext())
.load(item.getUrl())
.into(/*WHAT*/);
Any duplication would help.
You can achieve the same using Target or SimpleTarget (implements Target) in glide.
i.e.
Glide.load("http://somefakeurl.com/fakeImage.jpeg")
.asBitmap()
.fitCenter()
.into(new SimpleTarget(250, 250) {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
// Image Loaded successfully.
}
#Override
public void onLoadStarted(Drawable placeholder){
// Image Loading starts
}
#Override
public void onLoadFailed(Exception e, Drawable errorDrawable){
// Image Loading failed
}
});
}
lastly I did it
private ColorDrawable[] vibrantLightColorList =
{
new ColorDrawable(Color.parseColor("#9ACCCD")), new ColorDrawable(Color.parseColor("#8FD8A0")),
new ColorDrawable(Color.parseColor("#CBD890")), new ColorDrawable(Color.parseColor("#DACC8F")),
new ColorDrawable(Color.parseColor("#D9A790")), new ColorDrawable(Color.parseColor("#D18FD9")),
new ColorDrawable(Color.parseColor("#FF6772")), new ColorDrawable(Color.parseColor("#DDFB5C"))
};
then
Glide.with(holder.mImageView.getContext())
.load(item.getUrl())
.placeholder(getRandomDrawbleColor())
.into(holder.mImageView);
and
public ColorDrawable getRandomDrawbleColor() {
int idx = new Random().nextInt(vibrantLightColorList.length);
return vibrantLightColorList[idx];
}
Keep PlaceHolder Until The Image is Loading Using Picasso's placeholder function
Picasso.with(context)
.load("image_url")
.placeholder(R.drawable.ic_launcher)
.into(imageView);
Methods of setting the placeholder Image are same in Picasso and Glid
In Glid
Glide.with(holder.mImageView.getContext())
.load(item.getUrl())
.placeholder(R.drawable.placeholder)
.into(imageView)
In Picasso
Picasso.with(holder.mImageView.getContext())
.load(item.getUrl())
.placeholder(R.drawable.placeholder)
.into(imageView)
I use piccaso 2.5.2 and I have okhttp 2.4.0 and okhttp-urlconnection 2.4.0 in my gradle. The ImageView is a part custom list item layout. The error is that nothing appears in the ImageView. It occurs only for the first item of the list. That list is used to populate in SwipeFlingAdapterView which is similar to ListView. SwipeFlingAdapterView is from this library https://github.com/Diolor/Swipecards
Ok, here are my codes.
Picasso.with(mActivity)
.load(url)
.resize(540, 720)
.centerCrop()
.into(vh.iv_profile);
ImageView in xml file.
<ImageView
android:id="#+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#android:color/white"/>
The above code doesn't work so I try to log the error with this.
Picasso picasso = new Picasso.Builder(mActivity).listener(new Picasso.Listener() {
#Override public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
exception.printStackTrace();
Log.e("Picasso","description",exception);
}
}).build();
picasso.load(url)
.resize(540, 720)
.centerCrop()
.into(vh.iv_profile);
It doesn't show error. It doesn't even get into onImageLoadFailed. So, I try other way around.
picasso.load(url)
.resize(540, 720)
.centerCrop()
.into(vh.iv_profile, new Callback() {
#Override
public void onSuccess() {
if(vh.iv_profile.getHeight() == 0){
vh.iv_profile.setImageDrawable(mActivity.getResources().getDrawable(R.drawable.img_default));
Toast.makeText(mActivity, "we get here", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onError() {
}
});
OnSuccess in callback, I can't even set Image from drawable anymore. If I set Drawable before using picasso.load, I can set it successfully. I also tried to use it without .centerCrop(). It doesn't work.
here is the final code.
try {
Picasso.with(mActivity).load(url).into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.i("picasso error", "bitmap loaded");
if (bitmap != null) {
vh.iv_profile.setImageBitmap(bitmap);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Log.i("picasso error", width + ", " + height);
}
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
}catch(Exception e){
Log.i("picasso error", "exception");
}
onBitmapLoaded, I get a bitmap with width and height of 243 but it doesn't appear in the ImageView.
Please help!
Can anyone point me towards an example of how one might change an XML layout's background programatically using Picasso? All the examples I've found are able to update an ImageView using Picasso - but not a layout background.
Unable to set LinearLayout BackgroundResource from URL using Picasso
You can use Picasso's Target:
Picasso.with(this).load("http://imageUrl").into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
mYourLayout.setBackground(new BitmapDrawable(bitmap));
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
UPDATE
As #BladeCoder mentioned in the comment, Picasso holds a weak reference to Target objects, hence it is likely to be garbage collected.
So, following Jake Wharton's comment on one of the issues, i think this could be a good way to go:
CustomLayout mCustomLayout = (CustomLayout)findViewById(R.id.custom_layout)
Picasso.with(this).load("http://imageUrl").into(mCustomLayout);
CustomLayout.java:
public class CustomLayout extends LinearLayout implements Target {
public CustomLayout(Context context) {
super(context);
}
public CustomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
setBackground(new BitmapDrawable(getResources(), bitmap));
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
//Set your error drawable
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
//Set your placeholder
}
}
I use an ImageView as temporary ImageHolder. At first, load image with picasso into ImageView and set Layout Background from this ImageView by using getDrawable.
ImageView img = new ImageView(this);
Picasso.with(this)
.load(imageUri)
.fit()
.centerCrop()
.into(img, new Callback() {
#Override
public void onSuccess() {
myLayout.setBackgroundDrawable(img.getDrawable());
}
#Override
public void onError() {
}
});
None of the solutions above worked for me. But #Thiha's solution was the closest. The below worked for me:
final ImageView img = new ImageView(this);
Picasso.with(img.getContext()).load(url).into(img, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
collapsingToolbarLayout.setBackgroundDrawable(img.getDrawable());
}
#Override
public void onError() {
}
});
In my case i needed the image to fit the imageview's size so instead of loading the image in background i added this property to the imageview and loaded the image normally.
android:scaleType="fitXY"
As an alternative to previous answers. It's possible to set background via Piccasso simply by creating ImageView with match parent on the layout.
XML:
<ImageView
android:id="#+id/imageView_background"
android:layout_width="match_parent"
android:contentDescription="#string/background"
android:layout_height="match_parent"
android:alpha="0.7"/>
Kt:
Picasso.get().load(R.drawable.background_green_forest).fit().centerCrop().into(view.imageView_background)
You can try this, it works fine for me.
//ihave used releative layout in my activity
private RelativeLayout chatBG;
//initialize your layout
chatBG = findViewById(R.id.chat_activity);
//using picasso library, its better to use it on onStart() method to avoid some bugs
Picasso.get().load("https://your/url")
.placeholder(R.drawable.yourplaceholder_img)
.into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
chatBG.setBackground(new BitmapDrawable(bitmap));
}
#Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
Toast.makeText(ChatActivity.this, "Error : loading wallpaper", Toast.LENGTH_SHORT).show();
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});