gpt4 book ai didi

Android 从 URL 填充 ImageView

转载 作者:行者123 更新时间:2023-11-29 18:10:20 24 4
gpt4 key购买 nike

您好,我正在尝试从 URL 向 ImageView 添加图像,我已尝试将其作为位图加载,但没有显示任何内容。那么有谁知道最好的方法是什么或者我做错了什么?

这是我的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//Check Preferences which sets UI
setContentView(R.layout.singlenews);
TextView headerText = (TextView) findViewById(R.id.header_text);
headerText.setText("Latest News");


PostTask posttask;
posttask = new PostTask();
posttask.execute();
}

public void loadNews(){

newsStr = getIntent().getStringExtra("singleNews");




try {
JSONObject obj = new JSONObject(newsStr);
content = obj.getString("content");
title = obj.getString("title");
fullName = obj.getString("fullname");
created = obj.getString("created");
NewsImageURL = obj.getString("image_primary");
tagline = obj.getString("tagline");

meta = "posted by: " + fullName + " " + created;


URL aURL = new URL("NewsImageURL");
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
/* Buffered is always good for a performance plus. */
BufferedInputStream bis = new BufferedInputStream(is);
/* Decode url-data to a bitmap. */
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
/* Apply the Bitmap to the ImageView that will be returned. */






Log.v("lc", "content=" + content);
Log.v("lc", "title=" + title);
Log.v("lc", "fullname=" + fullName);
Log.v("lc", "created=" + created);
Log.v("lc", "NewsImage=" + NewsImageURL);
Log.v("lc", "Meta=" + meta);
Log.v("lc", "tagline=" + tagline);







} catch
(JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}




public class PostTask extends AsyncTask<Void, String, Boolean> {

@Override
protected Boolean doInBackground(Void... params) {
boolean result = false;
loadNews();
publishProgress("progress");
return result;
}

protected void onProgressUpdate(String... progress) {
StringBuilder str = new StringBuilder();
for (int i = 1; i < progress.length; i++) {
str.append(progress[i] + " ");

}
}

@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
Log.v("BGThread", "begin fillin data");

fillData();

}
}

public void fillData(){

NewsView = LayoutInflater.from(getBaseContext()).inflate(R.layout.newsdetailact,
null);

TextView Title = (TextView) NewsView.findViewById(R.id.NewsTitle);
Title.setText(title);

TextView Tagline = (TextView) NewsView.findViewById(R.id.subtitle);
Tagline.setText(tagline);

TextView MetaData = (TextView) NewsView.findViewById(R.id.meta);
MetaData.setText(meta);




ImageView NewsImage = (ImageView)NewsView.findViewById(R.id.imageView2);
NewsImage.setImageBitmap(bm);

TextView MainContent = (TextView) NewsView.findViewById(R.id.maintext);
MainContent.setText(content);





Log.v("BGThread", "Filled results");

adapter = new MergeAdapter();

adapter.addView(NewsView);


setListAdapter(adapter);


}



}

最佳答案

请使用此代码 spinet 将 url 图像转换为位图并在 ImageView 中显示。

URL url = new URL("http://image10.bizrate-images.com/resize?sq=60&uid=2216744464");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);

我认为它对您有帮助。

谢谢

关于Android 从 URL 填充 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11053085/

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