使用与毕加索的Getter图像 [英] Using Picasso with Image Getter
问题描述
我是个构建一个聊天应用程序,我试图将图像追加到一个EditText,通过使用毕加索来从一个URL和追加和ImageGetter将图像输出到图像附加到的EditText。然而,我在下面执行不起作用,因为使用时,应用程序什么也不显示(但确实的消息在数据库中显示)附加消息。
我一直没有使用的毕加索进行测试,因为只是简单地使用ImageGetter与应用程序内的图像资源工作得很好,只是它从一个URL作为要求不是。
什么是配置ImageGetter和/或追加方式,使这个功能将与毕加索工作的正确方法?还是有更简单的方法?
Append方法:
公共无效appendToMessageHistory(最后弦乐用户名,
最后弦乐消息){
如果(用户名= NULL&放大器;!&安培;!消息= NULL){ Picasso.with(getBaseContext())
.load(HTTP://本地主机:3000 /上传/运动/图像/ 2 / 2.JPG)
.into(新目标(){ @覆盖
在prepareLoad(绘制对象为arg0)公共无效{ } @覆盖
公共无效onBitmapLoaded(位图的位图,
LoadedFrom ARG1){
可绘制的drawImage =新BitmapDrawable(
getBaseContext()getResources(),位图)。 drawImage.setBounds(0,0,
drawImage.getIntrinsicHeight(),
drawImage.getIntrinsicWidth());
messageHistoryText.append(Html.fromHtml(< B>中
+用户名+:+&下; / B>中+< BR>中));
messageHistoryText.append(Html.fromHtml(消息
+<小时>中+< BR>中)
+ System.getProperty(line.separator)+); messageHistoryText.append(HTML
.fromHtml(< IMG SRC ='+的drawImage
+'/>中,
imageGetter,
空值));
} @覆盖
公共无效onBitmapFailed(绘制对象为arg0){ }
}); }
}
ImageGetter:
ImageGetter imageGetter =新ImageGetter(){
可绘制imageUsed = NULL; @覆盖
公众可绘制getDrawable(字符串源){ Picasso.with(getBaseContext())
.load(HTTP://本地主机:3000 /上传/运动/图像/ 2 / 2.JPG)
.into(新目标(){ @覆盖
在prepareLoad(绘制对象为arg0)公共无效{ } @覆盖
公共无效onBitmapLoaded(位图的位图,
LoadedFrom ARG1){
可绘制的drawImage =新BitmapDrawable(
getBaseContext()getResources(),位图)。 drawImage.setBounds(0,0,
drawImage.getIntrinsicHeight(),
drawImage.getIntrinsicWidth()); imageUsed =的drawImage; } @覆盖
公共无效onBitmapFailed(绘制对象为arg0){ }
}); 返回imageUsed;
} };
我无法得到它与使用毕加索的目标工作...
我的解决方法是:
- 使用的AsyncTask的并发
- 使用毕加索的get()方法来加载图像的同步在AsyncTask的背景方法
这样的:
公共类PicassoImageGetter实现Html.ImageGetter {最后的资源的资源;最后巴勃罗·毕加索;最终的TextView的TextView;公共PicassoImageGetter(最终的TextView TextView的,最终的资源的资源,最终巴勃罗·毕加索){
this.textView = TextView的;
this.resources =资源;
this.pablo =巴勃罗;
}@覆盖公共绘制对象getDrawable(最后弦乐源){
最终的结果BitmapDrawablePlaceHolder =新BitmapDrawablePlaceHolder(); 新的AsyncTask<无效,无效的,位图>(){ @覆盖
保护位图doInBackground(最终虚空... MEH){
尝试{
返回pablo.load(源)获得();
}赶上(例外五){
返回null;
}
} @覆盖
保护无效onPostExecute(最终位图位图){
尝试{
最后BitmapDrawable绘制=新BitmapDrawable(资源,位图); drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight()); result.setDrawable(绘制);
result.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight()); textView.setText(textView.getText()); //无效()无法正常工作......
}赶上(例外五){
/ *额定标称NOM * /
}
} } .execute((无效)NULL); 返回结果;
}静态类BitmapDrawablePlaceHolder扩展BitmapDrawable { 保护可绘制可绘制; @覆盖
公共无效画(最终帆布油画){
如果(绘!= NULL){
drawable.draw(画布);
}
} 公共无效setDrawable(绘制对象绘制){
this.drawable =绘制;
}}
希望这是非常有用的。
I ma building a chat application and I am attempting to append an image to an EditText, through use of Picasso to get the image from a URL and the append and ImageGetter to attach the image to the EditText. However, what I have implemented below does not work, as appending messages when using the app displays nothing (but the message does show up in the database).
I have tested without using Picasso, as simply just using the ImageGetter with an image resource within the app works just fine, only it's not from a URL as is required.
What is the proper way to configure the ImageGetter and/or the append method so that this functionality will work with Picasso? Or is there a simpler way?
Append method:
public void appendToMessageHistory(final String username,
final String message) {
if (username != null && message != null) {
Picasso.with(getBaseContext())
.load("http://localhost:3000/uploads/campaign/image/2/2.jpg")
.into(new Target() {
@Override
public void onPrepareLoad(Drawable arg0) {
}
@Override
public void onBitmapLoaded(Bitmap bitmap,
LoadedFrom arg1) {
Drawable drawImage = new BitmapDrawable(
getBaseContext().getResources(), bitmap);
drawImage.setBounds(0, 0,
drawImage.getIntrinsicHeight(),
drawImage.getIntrinsicWidth());
messageHistoryText.append(Html.fromHtml("<b>"
+ username + ":" + "</b>" + "<br>"));
messageHistoryText.append(Html.fromHtml(message
+ "<hr>" + "<br>")
+ System.getProperty("line.separator") + "");
messageHistoryText.append(Html
.fromHtml("<img src = '" + drawImage
+ "'/>",
imageGetter,
null));
}
@Override
public void onBitmapFailed(Drawable arg0) {
}
});
}
}
ImageGetter:
ImageGetter imageGetter = new ImageGetter() {
Drawable imageUsed=null;
@Override
public Drawable getDrawable(String source) {
Picasso.with(getBaseContext())
.load("http://localhost:3000/uploads/campaign/image/2/2.jpg")
.into(new Target() {
@Override
public void onPrepareLoad(Drawable arg0) {
}
@Override
public void onBitmapLoaded(Bitmap bitmap,
LoadedFrom arg1) {
Drawable drawImage = new BitmapDrawable(
getBaseContext().getResources(), bitmap);
drawImage.setBounds(0, 0,
drawImage.getIntrinsicHeight(),
drawImage.getIntrinsicWidth());
imageUsed=drawImage;
}
@Override
public void onBitmapFailed(Drawable arg0) {
}
});
return imageUsed;
}
};
I couldn't get it to work with using Picasso's Target...
My workaround is:
- use an AsyncTask for concurrency
- use Picasso's get() method to load the image synchronously in the AsyncTask's background method
Like this:
public class PicassoImageGetter implements Html.ImageGetter {
final Resources resources;
final Picasso pablo;
final TextView textView;
public PicassoImageGetter(final TextView textView, final Resources resources, final Picasso pablo) {
this.textView = textView;
this.resources = resources;
this.pablo = pablo;
}
@Override public Drawable getDrawable(final String source) {
final BitmapDrawablePlaceHolder result = new BitmapDrawablePlaceHolder();
new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(final Void... meh) {
try {
return pablo.load(source).get();
} catch (Exception e) {
return null;
}
}
@Override
protected void onPostExecute(final Bitmap bitmap) {
try {
final BitmapDrawable drawable = new BitmapDrawable(resources, bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
result.setDrawable(drawable);
result.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
textView.setText(textView.getText()); // invalidate() doesn't work correctly...
} catch (Exception e) {
/* nom nom nom*/
}
}
}.execute((Void) null);
return result;
}
static class BitmapDrawablePlaceHolder extends BitmapDrawable {
protected Drawable drawable;
@Override
public void draw(final Canvas canvas) {
if (drawable != null) {
drawable.draw(canvas);
}
}
public void setDrawable(Drawable drawable) {
this.drawable = drawable;
}
}
Hope this is useful.
这篇关于使用与毕加索的Getter图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!