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

贴纸炸弹

我有这个简单的程序,它从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;
     }
}
codeMagic

问题是

temp.setImageBitmap(image);

在背景内部Thread由于我们Thread除了无法更新UI元素以外,都无法更新UI元素,因此UI Thread我们需要在完成下载线程后发布回调,或使用AsyncTask具有进行后台工作的方法和在上进行工作的方法的UI Thread

此示例显示了AsyncTask的基本设置

AsyncTask文件

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

Android应用程序在简单应用程序中崩溃

来自分类Dev

在开始的Android应用程序中崩溃

来自分类Dev

在iOS应用程序中显示图像

来自分类Dev

从应用程序委托中检索managedObjectContext时崩溃

来自分类Dev

Android应用程序中的图像

来自分类Dev

当不选择裁剪图像应用程序时,阻止应用程序崩溃-Android

来自分类Dev

应用程序在图像选择上崩溃

来自分类Dev

Android:在应用程序中显示图标

来自分类Dev

在Android中显示正确的应用程序

来自分类Dev

从图库裁剪图像崩溃的应用程序-Android

来自分类Dev

Android从相机上传图像提供崩溃应用程序

来自分类Dev

在变量中设置图像时应用程序崩溃

来自分类Dev

从抽屉中动态加载图像使应用程序崩溃

来自分类Dev

显示通知后崩溃的应用程序

来自分类Dev

Android应用程序崩溃了,而没有崩溃显示在Firebase中

来自分类Dev

xCode:从应用程序文档目录中检索图像

来自分类Dev

从应用程序的本地文件夹中检索图像

来自分类Dev

Kotlin URL()。readText()使应用程序崩溃

来自分类Dev

显示图库中的图片时,应用程序崩溃

来自分类Dev

当 CachedNetworkImage 中未指定图像 url 时,应用程序崩溃

来自分类Dev

应用程序在 android studio 中的 managedQuery 行中崩溃

来自分类Dev

Proguard后Android应用程序崩溃

来自分类Dev

android Facebook SDK崩溃的应用程序?

来自分类Dev

Android应用程序在setText上崩溃

来自分类Dev

Android Map应用程序崩溃

来自分类Dev

暂停时Android应用程序崩溃

来自分类Dev

android应用程序在getString()行崩溃

来自分类Dev

Android WebView loadURL崩溃的应用程序?

Related 相关文章

热门标签

归档