android 下载图片到本地并显示

xiaoxiao2021-02-28  104

public class MainActivity extends AppCompatActivity implements View.OnClickListener {     protected Button butLoading;     protected Button butDisplay;     protected ImageView ima;     private URL murl;     Handler handler;     private static final String MIMG = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1499753534636&di=2599426532d38becb0406764cf463f0f&imgtype=0&src=http://dynamic-image.yesky.com/1080x-/uploadImages/2015/290/49/9ORK4DGNMWC7.jpg";     private HttpImgThread httpImgThread;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         super.setContentView(R.layout.activity_main); //主线程收到通知更新UI         handler = new Handler() {             @Override             public void handleMessage(Message msg) {                 super.handleMessage(msg);                 if(msg.what==1){                     ima.setImageBitmap((Bitmap) msg.obj);                 }             }         };         initView();     }    //点击下载并显示图片     @Override     public void onClick(View view) {         if (view.getId() == R.id.but_loading) {             imaLoading();         }      }     private void initView() {         butLoading = (Button) findViewById(R.id.but_loading);         butLoading.setOnClickListener(MainActivity.this);         butDisplay = (Button) findViewById(R.id.but_display);         butDisplay.setOnClickListener(MainActivity.this);         ima = (ImageView) findViewById(R.id.ima);     }     //下载图片     private void imaLoading() {         new Thread(new Runnable() {             @Override             public void run() {                 URL url = null;                 HttpURLConnection con = null;                 try {                     // 构造URL                     url = new URL(MIMG);                     // 打开连接                     con = (HttpURLConnection) url.openConnection();                     //请求方式                     con.setRequestMethod("GET");                     //设置超时时间                     con.setReadTimeout(5000);                     // 设置是否从httpUrlConnection读入,默认情况下是true(可以不写);                     con.setDoInput(true);                     //InputStream in = con.getInputStream();                     //存放路劲                     String parent = Environment.getExternalStorageDirectory()+"/yu/";                     File file1 = new File(parent);                     //不存在创建                     if(!file1.exists()){                         file1.mkdir();                     }                     File file = new File(file1, String.valueOf(System.currentTimeMillis()));                     // 输出的文件流                     FileOutputStream fos = new FileOutputStream(file);                     // 输入流                     InputStream in = con.getInputStream();                     // 2K的数据缓冲                     byte ch[] = new byte[2 * 1024];                     // 读取到的数据长度                     int len;                     if (fos != null) {                         // 开始读取                         while ((len = in.read(ch)) != -1) {                             fos.write(ch, 0, len);                         }                         // 完毕,关闭所有链接                         in.close();                         fos.close();                     }                     //根据本地绝对路径获取文件file.getAbsolutePath()                     final Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());                     //通知主线程更新UI                     Message obtain = Message.obtain();                     obtain.obj=bitmap;                     obtain.what=1;                     handler.sendMessage(obtain);                 } catch (MalformedURLException e) {                     e.printStackTrace();                 } catch (IOException e) {                     e.printStackTrace();                 }             }         }).start();     } }
转载请注明原文地址: https://www.6miu.com/read-66212.html

最新回复(0)