CallousToTheEnd 2015-08-12 15:18 采纳率: 100%
浏览 2000
已采纳

android中ImagView显示网络图片出错

初学就是想在ImageView上放一个网络图片,这代码哪里有问题,运行后会出错退出程序,在网上找了好多,但是代码我看着是一样的,为什么不行呢
activity代码:

 package com.lk.httptest;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.support.v7.app.ActionBarActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {

    ImageView imageView;
    Bitmap bmige;
    String imgurl = "http://c.hiphotos.baidu.com/image/pic/item/8c1001e93901213f3a7182d556e736d12f2e957d.jpg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.imageView);
        imageView.setImageBitmap(returnBitmap(imgurl));
    }

    public Bitmap returnBitmap(String url){
        URL myFileUrl = null;
        Bitmap bitmap = null;
        try {
            myFileUrl = new URL(url);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            URLConnection conn =  myFileUrl.openConnection();    
            conn.setDoInput(true);    
            conn.connect();    
            InputStream is = conn.getInputStream();    
            bitmap = BitmapFactory.decodeStream(is);    
            is.close();    

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

    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

XML布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView 
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
</RelativeLayout> 

已经在manifest中添加了标签

  • 写回答

6条回答 默认 最新

  • 胖子爱你520 2015-08-13 07:59
    关注

    这是首先要在AppContext配置ImageLoader这个类:

     public class AppContext extends Application {
    
        @Override
        public void onCreate() {
            super.onCreate();
            DisplayImageOptions options = new DisplayImageOptions.Builder()
                    .showStubImage(R.drawable.ic_launcher) 
                    .showImageForEmptyUri(R.drawable.ic_bu)
                    .showImageOnFail(R.drawable.ic_bu)
                    .cacheInMemory(true) 
                    .cacheOnDisk(true) 
                    // .displayer(new RoundedBitmapDisplayer(20)) 
                    .showImageOnLoading(R.drawable.ic_bu).build();
    
            ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                    getApplicationContext()).defaultDisplayImageOptions(options)
                    .threadPriority(Thread.NORM_PRIORITY - 2)
                    .denyCacheImageMultipleSizesInMemory()
                    .imageDownloader(new HttpUrlImageDownloader(this))
                    .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                    .tasksProcessingOrder(QueueProcessingType.LIFO).build();
    
            ImageLoader.getInstance().init(config);
        }
    }
    

    然后通过这样的方法:ImageLoader.getInstance().displayImage(imageURL, imageView);调用就可以加载网络图片了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥50 swiftui @query 报错
  • ¥50 怎么解决刷卡或扫码后,点击软件输入框,win10屏幕键盘不会自动弹出的问题
  • ¥15 如何使用arcgispro的训练深度模型,发现water和nowater精度为0?(相关搜索:深度学习)
  • ¥20 matlab作业不太懂呀有问题能给个代码吗
  • ¥15 自制电路图为何无法驱动ESP01S?
  • ¥15 前端加access数据库
  • ¥15 ARCGIS 多值提取到点 ERROR 999999
  • ¥15 mysql异常断电, [MY-011971] [InnoDB]
  • ¥15 uni.onBluetoothDeviceFound熄屏不运行
  • ¥15 求PHDA糖尿病并发症数据集,有偿