开发者社区> 问答> 正文

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中添加了标签

展开
收起
爵霸 2016-06-14 09:58:46 2118 0
1 条回答
写回答
取消 提交回答
  • 这是首先要在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);调用就可以加载网络图片了。

    2019-07-17 19:36:52
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载