zoukankan      html  css  js  c++  java
  • Android用ImageView显示本地和网上的图片

    Android用ImageView显示本地和网上的图片

    使用方法

    GetBitMap.getHttpBitmap(imgUrl, new GetBitMap.Callback() {
      @Override
      public void onSuccess(Bitmap bitmap) {
        imageView.setImageBitmap(bitmap);
      }
    });
    

    源码

    public class GetBitMap {
    
      public static Bitmap getLoacalBitmap(String url) {
        try {
          FileInputStream fis = new FileInputStream(url);
          return BitmapFactory.decodeStream(fis);
        } catch (FileNotFoundException e) {
          e.printStackTrace();
          return null;
        }
      }
    
      public static void getHttpBitmap(String url,Callback callback) {
        @SuppressLint("HandlerLeak") Handler handler = new Handler(){
          @Override
          public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            callback.onSuccess((Bitmap) msg.obj);
          }
        };
        new Thread(new Runnable() {
          @Override
          public void run() {
            URL myFileUrl = null;
            Bitmap bitmap = null;
            try {
              Log.d(TAG, url);
              myFileUrl = new URL(url);
            } catch (MalformedURLException e) {
              e.printStackTrace();
            }
            try {
              HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
              conn.setConnectTimeout(3000);
              conn.setDoInput(true);
              conn.connect();
              InputStream is = conn.getInputStream();
              bitmap = BitmapFactory.decodeStream(is);
              is.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
            Message message = new Message();
            message.obj = bitmap;
            handler.sendMessage(message);
          }
        }).start();
      }
    
      public interface Callback{
        void onSuccess(Bitmap bitmap);
      }
    }
    

    参考连接

    Android用ImageView显示本地和网上的图片

  • 相关阅读:
    响应式开发
    webstrom配置
    CSS水平垂直居中
    CSS3里的 转换与过渡动效
    CSS布局
    CSS定宽居中的实现方案
    Flex布局篇2
    编辑器中快速生成代码——emmet输入法
    display:flex实践加感悟
    websocket connet.js
  • 原文地址:https://www.cnblogs.com/guhunjun/p/android-imgview.html
Copyright © 2011-2022 走看看