Picasso not loading image, and no error logged - android

My usecase is to load images from a URL, and I am using picasso, in the following way to acheive that.
ll = (LinearLayout) findViewById(R.id.tasklayout);
ImageView imageView = new ImageView(this);
Picasso.with(this).load(taskFieldDao.data).into(imageView);
ll.addView(imageView, new LinearLayout.LayoutParams(800, 800));
What I see is a 800 * 800 whitespace with no image in it. Looking at the logs, there doesn't seem to any problem. Atleast picasso doesn't record anything.

I replicate your problem with button click and it is working. You can as well check the your path is correct
downloadButton = (Button)findViewById(R.id.download_button);
downloadButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative);
ImageView imageView = new ImageView(PicassoDownloadActivity.this);
Picasso.with(PicassoDownloadActivity.this).load(Helper.imageDownloadPath).into(imageView);
relativeLayout.addView(imageView, new LinearLayout.LayoutParams(800, 800));
}
});

Picasso won't typically give errors when something goes wrong, i suggest doing something like this:
ll = (LinearLayout) findViewById(R.id.tasklayout);
final ImageView imageView = new ImageView(this);
Picasso.with(this).load(taskFieldDao.data).into(imageView, new Callback() {
#Override
public void onSuccess() {
ll.addView(imageView, new LinearLayout.LayoutParams(800, 800));
}
#Override
public void onError() {
Log.e("Picasso","Image load failed);
}
});

Related

Show GIF using Glide in AlertDialog

I want to show GIF using Glide in ImageView, and set this ImageView in AlertDialog.
ImageView gifImageView = new ImageView(context);
Glide.with(context).load(recorder.getPictureFile()).asGif().into(gifImageView);
AlertDialog.Builder share_dialog = new AlertDialog.Builder(context);
share_dialog.setMessage("Live Message Saved");
share_dialog.setPositiveButton("Done", null);
share_dialog.setView(gifImageView);
share_dialog.show();
But it only shows Message and PositiveButton.
I can't understand why, what should I do to fix it ?
Update:2017/09/23
I create another project to test this issue, here is the code.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_main = (Button)findViewById(R.id.button_main);
imageView_main = (ImageView)findViewById(R.id.imageView_main);
GlideDrawableImageViewTarget imageViewMainTarget = new GlideDrawableImageViewTarget(imageView_main);
Glide.with(MainActivity.this).load(R.raw.test).into(imageViewMainTarget);
button_main.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ImageView gifImageView = new ImageView(MainActivity.this);
GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(gifImageView);
Glide.with(MainActivity.this).load(R.raw.test).into(imageViewTarget);
AlertDialog.Builder share_dialog = new AlertDialog.Builder(MainActivity.this);
share_dialog.setMessage("GIF Test");
share_dialog.setPositiveButton("Done", null);
share_dialog.setView(gifImageView);
share_dialog.show();
}
});
}
After running this project.
GIF can display in imageView_main, but it can't be shown on AlertDialog's gifImageView !
Why ?
I solved it, according to the answer of Niranj Patel.
How to put an image in an AlertDialog?? Android
Here is my code.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_main = (Button)findViewById(R.id.button_main);
imageView_main = (ImageView)findViewById(R.id.imageView_main);
GlideDrawableImageViewTarget imageViewMainTarget = new GlideDrawableImageViewTarget(imageView_main);
Glide.with(MainActivity.this).load(R.raw.test).into(imageViewMainTarget);
button_main.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater factory = LayoutInflater.from(MainActivity.this);
View view = factory.inflate(R.layout.sample, null);
ImageView gifImageView = (ImageView)view.findViewById(R.id.dialog_imageview);
GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(gifImageView);
Glide.with(MainActivity.this).load(R.raw.test).into(imageViewTarget);
AlertDialog.Builder share_dialog = new AlertDialog.Builder(MainActivity.this);
share_dialog.setMessage("GIF Test");
share_dialog.setPositiveButton("Done", null);
share_dialog.setView(view);
share_dialog.show();
}
});
}
Do it like this
ImageView gifImageView = new ImageView(context);
AlertDialog.Builder share_dialog = new AlertDialog.Builder(context);
share_dialog.setMessage("Live Message Saved");
share_dialog.setPositiveButton("Done", null);
share_dialog.setView(gifImageView);
Glide.with(context).load(recorder.getPictureFile()).asGif().into(gifImageView);
share_dialog.show();
put your load GLIDE after setting the view.

ImageView zooms on first tap

I am using a library to zoom in and out within a ImageView. I am using PhotoAttacher. When I click on the ImageView then first the ImageView loads in a fullscreen (within a dialog), and that is fine. But during loading in fullscreen mode the zoom function already takes place. How can I avoid zooming when I click first on ImageView. Here the code:
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog nagDialog = new Dialog(DetailView.this,
android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
nagDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
nagDialog.setCancelable(false);
nagDialog.setContentView(R.layout.preview_image);
Button btnClose = (Button) nagDialog
.findViewById(R.id.btnIvClose);
ivPreview = (ImageView) nagDialog
.findViewById(R.id.iv_preview_image);
// Loading image from url in ImageView
Picasso.with(getApplicationContext()).load(path)
.placeholder(R.drawable.loading).into(ivPreview);
// Here definition of zoom function
PhotoViewAttacher mAttacher = new PhotoViewAttacher(ivPreview);
mAttacher.canZoom();
btnClose.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
nagDialog.dismiss();
}
});
nagDialog.show();
}
});
The weird thing is, when I close the preview and click again on the imageview then it does not zoom in first.
You could implement the PhotoViewAttacher after they image is loaded successfully.
You add a callback to know if the request is successfully done.
Sample code:
Picasso.with(getApplicationContext()).load(path)
.placeholder(R.drawable.loading).into(ivPreview, new Callback(){
#Override
public void onSuccess()
{
Log.d("Test picasso", "image loaded");
PhotoViewAttacher mAttacher = new PhotoViewAttacher(ivPreview);
mAttacher.canZoom();
}
#Override
public void onError()
{
Log.d("Test picasso", "image error");
}
});
Be aware, the Callback param is a strong reference and will prevent your Activity or Fragment from being garbage collected. So you should invoke an adjacent Picasso.cancelRequest(android.widget.ImageView) call to prevent temporary leaking.

Programmatically create ImageView in Android

I followed this to create a simple animation.
The example creates manually an ImageView in the main layout, having a resource like this android:src="#anim/anim_android
The problem is, when I make a dynamic ImageView, and I set the resource like this myImageView.setImageResource(R.anim.anim_android);, the animation doesn't work, just views the first image of the sequence.
Any help?
Thanks.
you sould start the animation from you imageView :
myImageView.setImageResource(R.anim.anim_android);
final AnimationDrawable myAnimationDrawable = (AnimationDrawable)myImageViewn.getDrawable();
myImageView.post(
new Runnable(){
#Override
public void run() {
myAnimationDrawable.start();
}
});
}
EDIT : i think you didn't add the ImageView to your Layout ( contentView of your Activity ) ; try this :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout rootLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
rootLayout.setLayoutParams(params);
ImageView myAnimation = new ImageView(this);
RelativeLayout.LayoutParams paramsImage = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsImage.addRule(RelativeLayout.CENTER_IN_PARENT);
myAnimation.setLayoutParams(paramsImage);
myAnimation.setImageDrawable(getResources().getDrawable(R.anim.anim_android));
rootLayout.addView(myAnimation);
setContentView(rootLayout);
final AnimationDrawable myAnimationDrawable = (AnimationDrawable)myAnimation.getDrawable();
myAnimation.post(new Runnable() {
#Override
public void run() {
myAnimationDrawable.start();
}
});
}

dynamically added imageviews which one is clicked on?

I have a linear layout which I fill with imageviews everytime the user takes a picture with the camera. So these imageviews are added dynamically.
To each of these imageviews I attached an OnClick event to open the picture and show it in another imageview in another activity.
Each imageview has a tag containing an arraylist item with the bitmap information.
The OnClick event:
ImageView iv = new ImageView(this);
LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
iv.setLayoutParams(params);
iv.setImageBitmap(mBitmap);
iv.setTag(pli);
iv.setPadding(5, 5, 5, 5);
lvp.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
PhotoList pli = (PhotoList) arg0.getTag();
Intent i = new Intent(getBaseContext(), PhotoActivity.class);
i.putExtra("photo", pli.Photo);
i.putExtra("PhotoId", pli.id);
startActivity(i);
}
});
lvp.addView(iv);
Obviously the line with arg0.getTag() is not working.
Variable arg0 is of the linearlayout, but I need the clicked on imageview.
How do I detect wich imageview in the linearlayout was clicked on?
rg,
Eric
You have set the OnClickListener to the parent LinearLayout instead of the ImageView.
Instead of this:
lvp.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
...
}
});
Use this:
iv.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
...
}
});

Android is there ClickListener for linearLayout using addView?

I'm trying to create pinterest like layout. I find a way to achieve this: Android heterogeneous gridview like pinterest?!
However the problem is: I want to view item details after clicking each picture. But as I am using LinearLayout.addView() to add all the ImageViews, I'm not sure how I can get it clickable?
Is there anyway to be able to click each item on the view?
You can do this pretty easily by adding tag information to your image view that can be displayed when clicked.
Adding your image view would look like:
ImageView iv = new ImageView(context);
iv.setOnClickListener(your_listener);
iv.setTag("Item information");
linearLayout.addView(iv);
Then in your click listener:
public void onClick(View v) {
if(v instanceof ImageView) {
String info = (String)v.getTag();
/* Show information here */
}
}
Use this :
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// ADD your action here
}
});
or make your class implement the OnClickListner Interface and override the onClick() method
Use:
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do magic
}
});
And in your layout file mark the ImageView as clickable:
<ImageView
...
android:clickable="true">
...
</ImageView>
Building on the code that you linked, you can add a listener to each image view as you create it:
linear1 = (LinearLayout) findViewById(R.id.linear1);
linear2 = (LinearLayout) findViewById(R.id.linear2);
linear3 = (LinearLayout) findViewById(R.id.linear3);
for(int i=0;i<n;i++)
{
ImageView iv = new ImageView(this);
iv.setImageResource(R.id.icon);
iv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Your click code
}
}
int j = count % 3; <----
if(j==0)
linear1.addView(iv);
else if(j==1)
linear2.addView(iv);
else
linear3.addView(iv);
}

Categories

Resources