gpt4 book ai didi

java - 如何在ImageView java android上设置位图

转载 作者:行者123 更新时间:2023-12-01 22:33:06 25 4
gpt4 key购买 nike

我有一个代码,它从网址获取位图,然后更改 ImageView ,但我不知道为什么这不起作用。我尝试了一些类似的答案,但图像仍然没有设置。

这得到了一个位图。

class picture_get extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
URL img_value = null;
try {
img_value = new URL("https://graph.facebook.com/"+id+"picture?type=large");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
bitmap_pic = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
Log.i("bitmap_pic_get", "ok");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//bitmap_pic = Bitmap.createScaledBitmap(bitmap_pic, 50, 50, false);
return "Executed";
}
}

这设置了 ImageView

new picture_get().execute();

runOnUiThread(new Runnable() {

@Override
public void run() {
textView1.setText("Id: "+ id);
image1.setImageBitmap(bitmap_pic);
}

});

所有代码都有效,但图像仍然相同。

最佳答案

我建议在控制 ImageView 的类中实现一个接口(interface)。然后从 onPostExecute(Bitmap bmp) 调用它:

public class ImageDownloader
{
public interface ImageDownload
{
void getImage(Bitmap bmp);
}

Connection conn;
ImageDownload callBack;

public ImageDownloader(ImageDownload cb)
{
conn = new Connection();
this.callBack = cb;
}

public AsyncTask<String, Void, Bitmap> downloadFile(ObjectType objectType, int fileID, int width,
int height, int fitMode)
{
AsyncTask<String, Void, Bitmap> task = null;
String url = getURL(objectType,fileID,width,height,fitMode);
try
{
task = new Downloader().executeOnExecutor(
AsyncTask.THREAD_POOL_EXECUTOR, url);
}
catch (Exception e)
{
callBack.getImage(null);
}

return task;
}

String getURL(ObjectType objectType, int fileID, int width,
int height, int fitMode)
{
String url = conn.URL + FUNCTION
+ "?" + PARAM_OBJECT_TYPE + "=" + objectType.ObjectType()
+ "&" + PARAM_FILE_ID + "=" + fileID
+ "&" + PARAM_WIDTH + "=" + width
+ "&" + PARAM_HEIGHT + "=" + height
+ "&" + PARAM_FIT_MODE + "=" + fitMode;

return url;
}

private class Downloader extends AsyncTask<String, Void, Bitmap>
{
@Override
protected void onPostExecute(Bitmap b)
{
super.onPostExecute(b);
callBack.getImage(b);
}

@Override
protected Bitmap doInBackground(String... params)
{
//android.os.Debug.waitForDebugger();
Bitmap bmp = null;
try
{
URL url = new URL(params[0]);

URLConnection conn = url.openConnection();

conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setDoInput(true);


InputStream input = conn.getInputStream();
bmp = BitmapFactory.decodeStream(input);
} catch (MalformedURLException e) {

} catch (IOException e) {

}

return bmp;
}
}

}

关于java - 如何在ImageView java android上设置位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27334168/

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