Android 使用毕加索将图像放置在可绘制的图形中
我试图使用Picasso from Square从URL中提取jpg,然后附加到EditText。毕加索的原因是它的实现非常轻量级。可以看出,我使用的是占位符ImageView,毕加索将从提供的URL导入图像,然后我将该ImageView转换为可绘制视图。ImageGetter也是如此。但是,当使用下面的配置时,我收到空指针错误。(注意,当简单地使用应用程序资源中的drawable代替下面的'drawImage'变量时,此配置是有效的,但我试图扩展它以从URL中提取资源) 这里有什么不对劲的地方吗?还是一种更有效的方法 要附加到EditText的方法:Android 使用毕加索将图像放置在可绘制的图形中,android,nullpointerexception,picasso,Android,Nullpointerexception,Picasso,我试图使用Picasso from Square从URL中提取jpg,然后附加到EditText。毕加索的原因是它的实现非常轻量级。可以看出,我使用的是占位符ImageView,毕加索将从提供的URL导入图像,然后我将该ImageView转换为可绘制视图。ImageGetter也是如此。但是,当使用下面的配置时,我收到空指针错误。(注意,当简单地使用应用程序资源中的drawable代替下面的'drawImage'变量时,此配置是有效的,但我试图扩展它以从URL中提取资源) 这里有什么不对劲的地方
public void appendToMessageHistory(String username, String message) {
if (username != null && message != null) {
ImageView image = new ImageView(getApplicationContext());
Picasso.with(getBaseContext()).load("http://localhost:3000/uploads/campaign/image/2/2.jpg").into(image);
Drawable drawImage = image.getDrawable();
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));
}
}
}) 关于毕加索,你会错过一些东西。您可以在inTo方法中设置注释性目标类,如,并将位图设置为您拥有的任何对象:
Picasso.with(getBaseContext()).load("your url").into(new Target() {
@Override
public void onPrepareLoad(Drawable arg0) {
}
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
// TODO Create your drawable from bitmap and append where you like.
}
@Override
public void onBitmapFailed(Drawable arg0) {
}
});
编辑
这就是你如何做到的:
public void appendToMessageHistory(String username, String message) {
if (username != null && message != null) {
ImageView image = new ImageView(getApplicationContext());
Picasso.with(getBaseContext()).load("image url").into(new Target() {
@Override
public void onPrepareLoad(Drawable arg0) {
}
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
Drawable drawImage = new BitmapDrawable(getBaseContext().getResources(),bitmap);
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) {
}
});
}
}
public void appendToMessageHistory(字符串用户名、字符串消息){
如果(用户名!=null&&message!=null){
ImageView image=新的ImageView(getApplicationContext());
Picasso.with(getBaseContext()).load(“图像url”)。到(新目标()中{
@凌驾
准备加载时的公共无效(可提取arg0){
}
@凌驾
已加载位图(位图,从arg1加载){
Drawable drawImage=新的BitmapDrawable(getBaseContext().getResources(),位图);
messageHistoryText.append(Html.fromHtml(“+username+”):”
+“+”
”);
messageHistoryText.append(Html.fromHtml(message+“
”+“
”)
+System.getProperty(“line.separator”)+“”);
messageHistoryText.append(Html.fromHtml(““”),
imageGetter(空);
}
@凌驾
公共void onBitmapFailed(可提取arg0){
}
});
}
}
post you logcat for error.。如何使用此处概述的方法?类结构对我来说没有意义…只需填充onBitmapLoaded方法并在其中创建BitmapDrawable。然后像以前一样附加到文本编辑中。您不需要实现这里列出的所有方法。但他们都在自我解释。。onPrepareLoad在加载开始之前运行,并且有一个可绘制的参数(这是占位符图像),您可以详细介绍如何执行此操作吗?这不起作用,它指出:“类型new Target(){}的方法BitmapDrawable(参考资料,位图)未定义。还有其他方法吗?在第行BitmapDrawable(getBaseContext().getResources,位图);你能发布完整的错误日志吗?这没有道理。你不必使用imageGetter方法,因为你已经有了位图。你确定你使用的是正确的“目标”(导入com.squareup.picasso.Target)吗
public void appendToMessageHistory(String username, String message) {
if (username != null && message != null) {
ImageView image = new ImageView(getApplicationContext());
Picasso.with(getBaseContext()).load("image url").into(new Target() {
@Override
public void onPrepareLoad(Drawable arg0) {
}
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
Drawable drawImage = new BitmapDrawable(getBaseContext().getResources(),bitmap);
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) {
}
});
}
}