Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageLoader with fragments #7

Closed
thest1 opened this issue Feb 2, 2012 · 13 comments
Closed

ImageLoader with fragments #7

thest1 opened this issue Feb 2, 2012 · 13 comments

Comments

@thest1
Copy link
Owner

thest1 commented Feb 2, 2012

Several people have reported me that ImageLoader doesn't display bitmaps correctly if used inside fragments. Unfortunately I can't reproduce this problem - it works just fine for me. It would be great if someone could attach a demo project with the problem inside. Have some ideas how to fix it. But before applying the fix need to be able to reproduce the problem and test it well.

@ghost
Copy link

ghost commented Feb 6, 2012

Hi Fedor, I used your lib in my project inside the fragment with a gridview. And it works just perfect.

Best regards, Nikolay

@dm77
Copy link

dm77 commented Feb 17, 2012

Hi Fedor,

Like I promised, here is a demo app: https://github.com/DushyanthMaguluru/LazyListFragment that fails to set images correctly. I used a Cursor adapter in combination with a ContentProvider. You should notice that once you click on "Launch UserList" button on the main view, all the images are set to the default image. Only if you scroll down to the bottom and scroll back up, you will see the images being refreshed. And if you look at the logcat you will notice that you will only see these lines:

[ VERBOSE ] => ImageLoader: BitmapDisplayer Initialized
[ VERBOSE ] => ImageLoader: BitmapDisplayer Initialized
[ VERBOSE ] => ImageLoader: BitmapDisplayer Initialized
[ VERBOSE ] => ImageLoader: BitmapDisplayer Initialized
[ VERBOSE ] => ImageLoader: BitmapDisplayer Initialized

Note: I also tried setting the tag like you recommended on a different ticket, image.setTag(url). The results didn't change though.

Finally, I am sorry if there is actually a mistake in my implementation.

Thanks,
Dushyanth

@thest1
Copy link
Owner Author

thest1 commented Feb 19, 2012

Dushyanth, thank you very much for the code.
I can see what's wrong with it. You pass application context to the adapter. That's why list items are inflated in application context and their getContext() is application not activity. Here's a fix https://github.com/thest1/LazyListFragment/commit/b15c2f635f90ed1b7c9eacd7357d2a077c4847c7.

Now the question is should I make a fix to allow ImageLoader work even under application context not just activity context. I'll have to create additional handlers for that as proposed by Vladislav gingo@e9e2c83. I'm not sure about that yet. Actually if you use ImageLoader you always have Activity and you can pass it not an application.

@dm77
Copy link

dm77 commented Feb 19, 2012

Fedor, Thanks so much for correcting my mistake and proving that I am stupid :D. I am sorry to have wasted your time.

@thest1
Copy link
Owner Author

thest1 commented Feb 19, 2012

Don't worry, you just helped me to figure out a weak place that can cause problems for many other people. So you contribution is highly appreciated.

@dm77
Copy link

dm77 commented Feb 19, 2012

Thank you. As for changing the ImageLoader to use Handlers, I agree with your point that ImageLoader always has an activity. So not sure if its worth implementing the Handler architecture.

@johnnycube
Copy link

Hi Fedor

I just recently ran into your mail regarding this issue and just became curious about testing in which situations Lazylist works with Fragments. I then saw, that this was covered here already ;)

But I tested your idea intensivly about using ImageView.post(Runnable r) and it works like a charm.

  photoToLoad.imageView.post(bd);

instead of

  Activity a=(Activity)photoToLoad.imageView.getContext();
        a.runOnUiThread(bd);

Works like a charm in every situation i encountered.

I also implemented a animation for showing the images. If your interested let me know.

With best regards

@thest1
Copy link
Owner Author

thest1 commented Mar 12, 2012

I don't use post() any more. Official documentation ways post() will not
work when view is not attached to window
http://developer.android.com/reference/android/view/View.html#post(java.lang.Runnable).
And I really faced this issue. When I just started the application several
first images are just not displayed. Post() is not reliable.

If you have a nice animations code you can form my project on github and
commit your modifications. This way me or anybody else will be able to
reuse your modifications. Works for you?

12 марта 2012 г. 2:20 пользователь johnnycube <
reply@reply.github.com

написал:

Hi Fedor

I just recently ran into your mail regarding this issue and just became
curious about testing in which situations Lazylist works with Fragments. I
then saw, that this was covered here already ;)

But I tested your idea intensivly about using ImageView.post(Runnable r)
and it works like a charm.

 photoToLoad.imageView.post(bd);

instead of

 Activity a=(Activity)photoToLoad.imageView.getContext();
       a.runOnUiThread(bd);

Works like a charm in every situation i encountered.

I also implemented a animation for showing the images. If your interested
let me know.

With best regards


Reply to this email directly or view it on GitHub:
#7 (comment)

@segundoacosta
Copy link

Hi Fedor

public class MyFragment extends Fragment{

ImageLoader imgtmp;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 

    imgtmp= new ImageLoader ( getActivity().getApplicationContext() );

}

}

I'm doing everything from a fragment and I boot ERROR on line:

Activity a=(Activity)photoToLoad.imageView.getContext();

any solution to this?

@thest1
Copy link
Owner Author

thest1 commented Dec 6, 2012

I happens because getContext() returns Application context not Activity context. I have just commited a fix, please try it 232d7f6. It uses hander to run the UI job so it doesn't depend on Activity. Should work fine for you now.

@segundoacosta
Copy link

Thank you!

works well, the only thing is that the images are too small, see if there's something wrong with my code, thanks for the support

@thest1 thest1 closed this as completed Dec 6, 2012
@demo-Ashif
Copy link

I'm getting error "Can't create handler inside thread that has not called looper.prepare() "
Please someone suggest.

@thest1
Copy link
Owner Author

thest1 commented Sep 24, 2014

Probably because you create ImageLoader in some thread that is not UI thread.

@demo-Ashif
Copy link

thanks , my problem is solved. I have one more issue. I want to resize my downloaded bitmap image . Alrdy seen this issue #19 and you suggested to disable it if dont need it. I just want to show image as 64 X 64 , so wht should i do ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants