1 1 save.setOnClickListener(new View.OnClickListener() { 2 2 @Override 3 3 public void onClick(View v) { 4 4 Glide.with(itemActivity.this) 5 5 .asBitmap() 6 6 .load(url) 7 7 .into(new SimpleTarget<Bitmap>() { 8 8 @Override 9 9 public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) { 1010 saveImage(resource); 1111 } 1212 }); 1313 1414 } 1515 }); 1616 1717 private void saveImage(Bitmap image) { 1818 String saveImagePath = null; 1919 Random random = new Random(); 2020 String imageFileName = "JPEG_" + "down" + random.nextInt(10) + ".jpg"; 2121 File storageDir = new File(Environment.getExternalStoragePublicDirectory 2222 (Environment.DIRECTORY_PICTURES) + "test"); 2323 2424 boolean success = true; 2525 if(!storageDir.exists()){ 2626 success = storageDir.mkdirs(); 2727 } 2828 if(success){ 2929 File imageFile = new File(storageDir, imageFileName); 3030 saveImagePath = imageFile.getAbsolutePath(); 3131 try { 3232 OutputStream fout = new FileOutputStream(imageFile); 3333 image.compress(Bitmap.CompressFormat.JPEG, 100, fout); 3434 fout.close(); 3535 } catch (Exception e) { 3636 e.printStackTrace(); 3737 } 3838 3939 // Add the image to the system gallery 4040 galleryAddPic(saveImagePath); 4141 Toast.makeText(mContext, "IMAGE SAVED", Toast.LENGTH_LONG).show(); 4242 } 4343 // return saveImagePath; 4444 } 4545 4646 private void galleryAddPic(String imagePath) { 4747 Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 4848 File f = new File(imagePath); 4949 Uri contentUri = Uri.fromFile(f); 5050 mediaScanIntent.setData(contentUri); 5151 sendBroadcast(mediaScanIntent); 5252 }
Android:通过Glide保存图片到本地,并同步到相册
Stella981
2021-10-11
2150 0 0
点赞
收藏
评论区
加载中...