How to get URI on imageview with Glide

前端 未结 1 1999
我寻月下人不归
我寻月下人不归 2021-01-13 03:44

I\'m using \'Glide\' to load image from a server to an ImageView. I would like to know if it\'s possible to extract that URI from the imageview itself.

Image         


        
1条回答
  •  孤街浪徒
    2021-01-13 04:16

    to get Uri change ur Glide implementation to:

    Bitmap bitmap;
    
     Glide.with(getApplicationContext())
                    .load(contentImageUrl)
                    .asBitmap()
                    .into(new SimpleTarget() {
                        @Override
                        public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
                            // you can do something with loaded bitmap here
                            contentImage.setImageBitmap(resource);
                            bitmap= resource;
                        }
                    });
    

    Now call the method;

        //get URI from bitmap
       Uri uri = getImageUri(getApplicationContext(),bitmap);
    

    in getImageUri function:

    public Uri getImageUri(Context inContext, Bitmap inImage) {
      ByteArrayOutputStream bytes = new ByteArrayOutputStream();
      inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
      String path = MediaStore.Images.Media.insertImage(MainActivity.this.getContentResolver(), inImage, UUID.randomUUID().toString() + ".png", "drawing");
      return Uri.parse(path);
    } 
    

    You need to add permission in manifest:

    
    

    Also for runtime permission: Api>=23

    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
    

    0 讨论(0)
提交回复
热议问题