zoukankan      html  css  js  c++  java
  • Android:通过Glide保存图片到本地,并同步到相册

     1 save.setOnClickListener(new View.OnClickListener() {
     2                 @Override
     3                 public void onClick(View v) {
     4                     Glide.with(itemActivity.this)
     5                             .asBitmap()
     6                             .load(url)
     7                             .into(new SimpleTarget<Bitmap>() {
     8                                 @Override
     9                                 public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
    10                                     saveImage(resource);
    11                                 }
    12                             });
    13 
    14                 }
    15             });
    16 
    17 private void saveImage(Bitmap image) {
    18         String saveImagePath = null;
    19         Random random = new Random();
    20         String imageFileName = "JPEG_" + "down" + random.nextInt(10) + ".jpg";
    21         File storageDir = new File(Environment.getExternalStoragePublicDirectory
    22                 (Environment.DIRECTORY_PICTURES) + "test");
    23 
    24         boolean success = true;
    25         if(!storageDir.exists()){
    26             success = storageDir.mkdirs();
    27         }
    28         if(success){
    29             File imageFile = new File(storageDir, imageFileName);
    30             saveImagePath = imageFile.getAbsolutePath();
    31             try {
    32                 OutputStream fout = new FileOutputStream(imageFile);
    33                 image.compress(Bitmap.CompressFormat.JPEG, 100, fout);
    34                 fout.close();
    35             } catch (Exception e) {
    36                 e.printStackTrace();
    37             }
    38 
    39             // Add the image to the system gallery
    40             galleryAddPic(saveImagePath);
    41             Toast.makeText(mContext, "IMAGE SAVED", Toast.LENGTH_LONG).show();
    42         }
    43 //        return saveImagePath;
    44     }
    45 
    46     private void galleryAddPic(String imagePath) {
    47         Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    48         File f = new File(imagePath);
    49         Uri contentUri = Uri.fromFile(f);
    50         mediaScanIntent.setData(contentUri);
    51         sendBroadcast(mediaScanIntent);
    52     }
  • 相关阅读:
    js调用后台方法
    Js 实现trim方法
    Service获取客户端IP地址(java)
    ASP.NET中页面传值
    Asp.net 实现选择文件批量下载
    js调用后台代码
    获取webservice客户端IP地址 (C# java )
    使 WebBrowser 更简单的新加和执行 js, 可安装 jQuery 脚本的 C# 开源代码 IEBrowse...
    Mac下各种网络命令的使用
    Java关键字
  • 原文地址:https://www.cnblogs.com/Rainm/p/10547563.html
Copyright © 2011-2022 走看看