【问题标题】:How to save image to internal storage with Glide?如何使用 Glide 将图像保存到内部存储?
【发布时间】:2019-05-19 15:43:47
【问题描述】:

在我的应用程序中,我创建了一个图像滑块,并使用 Glide 将图像显示到 imageview 中。有没有办法将按钮单击时的图像保存到内部存储?

我环顾四周,我想我必须先将图像加载为位图,然后将其保存到内部存储中,但我想知道如何实现这一点。

提前谢谢你。

编辑:你们认为我的问题是重复的。我已经尝试过save pictures in device [Glide Library] ,但SimpleTarget<Bitmap> 已被弃用。

【问题讨论】:

    标签: android imageview android-glide


    【解决方案1】:

    由于不推荐使用 SimpleTarget。您可以通过以下示例获取位图。

      try{
    
                  Glide.with(mContext)
                  .asBitmap().load(live_marker_img)
                  .apply(new RequestOptions()
                  .override(150,150)
                  .diskCacheStrategy(DiskCacheStrategy.ALL))
                  .listener(new RequestListener<Bitmap>(){
    
      @Override
       public boolean onLoadFailed(@Nullable GlideException e,Object 
        o,Target<Bitmap> target,boolean b){
    
            return false;
            }
    
        @Override
       public boolean onResourceReady(Bitmap bitmap,Object o,Target<Bitmap> 
        target,DataSource dataSource,boolean b){
    
            Bitmap bitmap=resource;   // you will get bitmap here
    
            saveImage(bitmap);   // save your bitmap
            return false;
            }
            }
            ).submit();
    
    
            }catch(Exception e){
            e.printStackTrace();
            }
    

    那么保存位图的方法是-

    private String saveImage(Bitmap image) {
        String savedImagePath = null;
    
        String imageFileName = "JPEG_" + "FILE_NAME" + ".jpg";
        File storageDir = new File(            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                           + "/YOUR_FOLDER_NAME");
        boolean success = true;
        if (!storageDir.exists()) {
        success = storageDir.mkdirs();
        }
        if (success) {
            File imageFile = new File(storageDir, imageFileName);
            savedImagePath = imageFile.getAbsolutePath();
            try {
                OutputStream fOut = new FileOutputStream(imageFile);
                image.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
                fOut.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            // Add the image to the system gallery
            galleryAddPic(savedImagePath);
            Toast.makeText(mContext, "IMAGE SAVED", Toast.LENGTH_LONG).show();
        }
        return savedImagePath;
    }
    
    private void galleryAddPic(String imagePath) {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(imagePath);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        sendBroadcast(mediaScanIntent);
    }
    

    【讨论】:

    • 资源变量在哪里定义。第一个代码块中的那个是什么
    猜你喜欢
    • 2017-11-29
    • 1970-01-01
    • 2021-09-24
    • 2013-10-28
    • 2017-12-11
    • 1970-01-01
    • 2016-08-06
    • 2018-04-20
    • 1970-01-01
    相关资源
    最近更新 更多