gpt4 book ai didi

android - 尝试使用位图从 Url 设置 ImageView

转载 作者:行者123 更新时间:2023-11-30 00:36:09 25 4
gpt4 key购买 nike

我正在尝试设置我的 ImageView。我有图像的 url 并试图将其制作成位图,然后将此位图设置到我的 ImageView。但是,位图结果(onPostExecute 的参数)从 download_Image 函数中返回为 null。这意味着' BitmapFactory.decodeStream(is); ' 正在返回 null。

this.ImageView1 = (ImageView) infoWindow.findViewById(R.id.ImageView1);
ImageView1.setTag(URL);

new AsyncTask<ImageView, Void, Bitmap>(){
ImageView imageView = null;

@Override
protected Bitmap doInBackground(ImageView... imageViews) {
final Bitmap[] b = new Bitmap[1];
this.imageView = imageViews[0];

activity.runOnUiThread(new Runnable() {
@Override
public void run() {
b[0] =download_Image((String) imageView.getTag());
}
});

return b[0];

}

@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}

public Bitmap download_Image(String url) {

//---------------------------------------------------

URL newurl = null;

try {
newurl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}

Bitmap mIcon_val = null;

try {
URLConnection urlConnection=newurl.openConnection();
InputStream is=urlConnection.getInputStream();
mIcon_val=BitmapFactory.decodeStream(is);
} catch (Exception e) {
e.printStackTrace();
}

return mIcon_val;

//---------------------------------------------------

}

}.execute(ImageView1);

我该如何处理这个问题?

最佳答案

如果有任何问题,请使用此代码并进行评论`public class MainActivity extends AppCompatActivity {

private ImageView imageView;
private String imageUrl ="image url";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imageView = (ImageView)findViewById(R.id.imageview);

new AsyncTask<ImageView, Void, Bitmap>(){
ImageView imageView = null;

@Override
protected Bitmap doInBackground(ImageView... imageViews) {
final Bitmap[] b = new Bitmap[1];
this.imageView = imageViews[0];

b[0] =download_Image(imageUrl);

return b[0];

}

@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}

public Bitmap download_Image(String url) {

//---------------------------------------------------

URL newurl = null;

try {
newurl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}

Bitmap mIcon_val = null;

try {
URLConnection urlConnection=newurl.openConnection();
InputStream is=urlConnection.getInputStream();
mIcon_val= BitmapFactory.decodeStream(is);
} catch (Exception e) {
e.printStackTrace();
}

return mIcon_val;

//---------------------------------------------------

}

}.execute(imageView);}}`

关于android - 尝试使用位图从 Url 设置 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43392946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com