NetworkOnMainThreadException with Html.fromHtml in Html.ImageGetter
I want to load an image in TextView by setText(Html.fromHtml(htmlString, ImageGetter)); But I get exception
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
public class ImageGetter implements Html.ImageGetter {
@Override
public Drawable getDrawable(String source) {
Bitmap bitmap = ImageLoader.getInstance().loadImageSync(source);
Drawable d = new BitmapDrawable(Resources.getSystem(), bitmap);
Log.v("", source);
return d;
}
}
I use above code within an adapter,should I use just like that? https://github.com/gonjay/rubychina4android/blob/master/app/src/main/java/org/rubychina/app/ui/adapter/TopicReplyAdapter.java
The exception is caused by StrictMode setting. This exception would disappear when you close the strict mode.
It is not recommended to load image from network on main thread because network access is usually slow which would block the smooth operation.
Thank you. I just realized loadImageSync is a Sync method...
Dose UIL has a loadImageAsync or like method?
loadImage is what u want. Try It!
I find a solution:http://stackoverflow.com/questions/15617210/android-html-fromhtml-with-images But thank you anyway!
@gonjay that's unfortunately not a good one. you can try mine : https://github.com/virtualvoid/AsyncImageGetter
i'll update it to latest version soon.
@virtualvoid Cool~~~~!! I'll try yours right now
getDrawable is a synchronized method. you should return drawable right now (extra one or placeholder ), if not found in memory , then load it with load(TextView tv, String source){...}.
This operation in main thread always cause NetworkOnMainThreadException I think~ Bitmap bitmap = ImageLoader.getInstance().loadImageSync(source); ImageView.setXXX(bitmap);