gpt4 book ai didi

java - 应用程序从 URL 检索图像并在 Android 应用程序中显示时崩溃

转载 作者:行者123 更新时间:2023-12-02 00:00:12 25 4
gpt4 key购买 nike

我有一个简单的程序,它从 URL 检索图像 ----- 以位图保存 --- 显示为 ImageView 。但是,当我尝试运行它时,它不断崩溃。我有 downloadBitmap 方法将 url 转换为位图,然后我想将图像设置为 ImageBitmap?

package com.example.bitmapdisplay;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.fasterxml.jackson.core.JsonParseException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class MainActivity extends Activity {

Bitmap image;
BitmapDrawable bd;
ImageView temp;
ProgressDialog pd;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

temp = (ImageView) findViewById(R.id.ivImage);


Thread retrieveImage = new Thread() {
public void run(){
try {

image = downloadBitmap("http://i1.cpcache.com/product_zoom/617914535/dickbutt_2_mug.jpg?side=Back&height=250&width=250&padToSquare=true");

} catch (JsonParseException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}
finally {

temp.setImageBitmap(image);
}
}
};
retrieveImage.run();

}

private Bitmap downloadBitmap(String url) throws JsonParseException, IOException{

// initilize the default HTTP client object
final DefaultHttpClient client = new DefaultHttpClient();

//forming a HttoGet request
final HttpGet getRequest = new HttpGet(url);

HttpResponse response = client.execute(getRequest);

//check 200 OK for success
final int statusCode = response.getStatusLine().getStatusCode();

if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode +
" while retrieving bitmap from " + url);
return null;

}

final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
// getting contents from the stream
inputStream = entity.getContent();

// decoding stream data back into image Bitmap that android understands
image = BitmapFactory.decodeStream(inputStream);


} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}

return image;
}
}

最佳答案

问题在于

temp.setImageBitmap(image);

位于后台线程内部。由于我们无法更新除 UI Thread 之外的任何 Thread 上的 UI 元素,我们需要在下载线程完成时发布回调或使用 AsyncTask 它具有执行后台工作的方法和在 UI 线程 上执行工作的方法。

This example shows the basic set up for an AsyncTask

AsyncTask Docs

关于java - 应用程序从 URL 检索图像并在 Android 应用程序中显示时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23961197/

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