您的位置:首页 > 理论基础 > 计算机网络

Android三种基本的加载网络图片方式

2017-02-08 18:30 405 查看
Android三种基本的加载网络图片方式,包括普通加载网络方式、用ImageLoader加载图片、用Volley加载图片。

普通加载网络方式:

public class NormalLoadPictrue {

     

    private String uri;

    private ImageView imageView;

    private byte[] picByte;

     

     

    public void getPicture(String uri,ImageView imageView){

        this.uri = uri;

        this.imageView = imageView;

        new Thread(runnable).start();

    }

     

    @SuppressLint("HandlerLeak")

    Handler handle = new Handler(){

        @Override

        public void handleMessage(Message msg) {

            super.handleMessage(msg);

            if (msg.what == 1) {

                if (picByte != null) {

                    Bitmap bitmap = BitmapFactory.decodeByteArray(picByte, 0, picByte.length);

                    imageView.setImageBitmap(bitmap);

                }

            }

        }

    };

 

    Runnable runnable = new Runnable() {

        @Override

        public void run() {

            try {

                URL url = new URL(uri);

                HttpURLConnection conn = (HttpURLConnection)url.openConnection();

                conn.setRequestMethod("GET");

                conn.setReadTimeout(10000);

                 

                if (conn.getResponseCode() == 200) {

                    InputStream fis =  conn.getInputStream();

                    ByteArrayOutputStream bos = new ByteArrayOutputStream();

                    byte[] bytes = new byte[1024];

                    int length = -1;

                    while ((length = fis.read(bytes)) != -1) {

                        bos.write(bytes, 0, length);

                    }

                    picByte = bos.toByteArray();

                    bos.close();

                    fis.close();

                     

                    Message message = new Message();

                    message.what = 1;

                    handle.sendMessage(message);

                }

                 

                 

            }catch (IOException e) {

                e.printStackTrace();

            }

        }

    };

     
}


用ImageLoader加载图片

public class ImageLoaderPicture {

     

    private DisplayImageOptions options;

 

    public ImageLoaderPicture(Context context) {

         

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).threadPriority(Thread.NORM_PRIORITY - 2)

        .denyCacheImageMultipleSizesInMemory()

        .discCacheFileNameGenerator(new Md5FileNameGenerator())

        .tasksProcessingOrder(QueueProcessingType.LIFO).enableLogging() 

        .memoryCache(new WeakMemoryCache())                                 

        .build();

        ImageLoader.getInstance().init(config);

         

        options = new DisplayImageOptions.Builder()

        .showStubImage(0)

        .showImageForEmptyUri(0)

        .showImageOnFail(0)

        .cacheInMemory().cacheOnDisc()

        .imageScaleType(ImageScaleType.IN_SAMPLE_INT)

        .bitmapConfig(android.graphics.Bitmap.Config.RGB_565)

        .build();

    }

 

    public DisplayImageOptions getOptions() {

        return options;

    }

 

    public void setOptions(DisplayImageOptions options) {

        this.options = options;

    }

     

     

}


用Volley加载图片

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: