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

时间:2014-05-30 18:30:16

标签: java android url bitmap crash

我有这个简单的程序从URL中检索图像-----在位图保存---显示为图像视图。 但是,当我尝试运行它时,它会继续崩溃。我有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;
     }
}

1 个答案:

答案 0 :(得分:0)

问题在于

temp.setImageBitmap(image);

位于背景Thread内。由于我们无法在Thread以外的任何UI Thread更新UI元素,因此我们需要在下载线程完成时发布回调,或使用具有以下方法的AsyncTask做后台工作和在UI Thread上完成工作的方法。

This example shows the basic set up for an AsyncTask

AsyncTask Docs