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))),用于更新图库

原文地址:https://www.cnblogs.com/buyishi/p/10574195.html