zoukankan      html  css  js  c++  java
  • Android笔记之使用ImageView加载网络图片以及保存图片到本地并更新图库

    ImageView显示网络图片

            findViewById(R.id.btnLoad).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new Thread() {
                        @Override
                        public void run() {
                            super.run();
                            try (InputStream inputStream = new URL(URL_NETWORK_PIC).openStream()) {
                                final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        ivNetworkPic.setImageBitmap(bitmap);
                                    }
                                });
                            } catch (IOException ex) {
                                Log.e(TAG, null, ex);
                            }
                        }
                    }.start();
                }
            });

    保存图片到本地并更新图库

            ivNetworkPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AndPermission.with(MainActivity.this).runtime().permission(Permission.WRITE_EXTERNAL_STORAGE)
                            .onGranted(new Action<List<String>>() {
                                @Override
                                public void onAction(List<String> data) {
                                    new Thread() {
                                        @Override
                                        public void run() {
                                            super.run();
                                            File directory = new File(Environment.getExternalStorageDirectory(), getString(R.string.app_name));
                                            if (!directory.exists()) {
                                                directory.mkdir();
                                            }
                                            String filename = System.currentTimeMillis() + ".png";
                                            File file = new File(directory, filename);
                                            try (FileOutputStream fileOutputStream = new FileOutputStream(file); InputStream inputStream = new URL(URL_NETWORK_PIC).openStream()) {
                                                byte[] buffer = new byte[10240];
                                                int byteCount;
                                                while ((byteCount = inputStream.read(buffer)) != -1) {
                                                    fileOutputStream.write(buffer, 0, byteCount);
                                                }
                                                sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
                                                runOnUiThread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        Toast.makeText(MainActivity.this, "图片已保存", Toast.LENGTH_SHORT).show();
                                                    }
                                                });
                                            } catch (final IOException ex) {
                                                Log.e(TAG, null, ex);
                                                runOnUiThread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        Toast.makeText(MainActivity.this, "图片保存失败:" + ex, Toast.LENGTH_SHORT).show();
                                                    }
                                                });
                                            }
                                        }
                                    }.start();
                                }
                            })
                            .onDenied(new Action<List<String>>() {
                                @Override
                                public void onAction(List<String> data) {
                                    new AlertDialog.Builder(MainActivity.this).setMessage("没有写入外部存储的权限");
                                }
                            }).start();
                }
            });

    其中sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file))),用于更新图库

  • 相关阅读:
    有关创造力的书籍推荐
    如何做好组织管理?
    EMBA方面的经典自学教材、书籍推荐
    人力资源管理书单推荐
    创新创业类书籍推荐
    企业如何进行组织变革?
    VMS(VELO) tips[转载]
    SAP R/3系统的概念与介绍
    Project Record: RCM Program–Change Delivery Date in VMS Action[转载]
    SQL Server触发器创建、删除、修改、查看示例步骤
  • 原文地址:https://www.cnblogs.com/buyishi/p/10574195.html
Copyright © 2011-2022 走看看