How to get URI on imageview with Glide

ぐ巨炮叔叔 提交于 2019-12-01 06:47:37

问题


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.

ImageView contentImage = (ImageView) findViewById(R.id.contentImage);

........

Glide.with(getApplicationContext()).load(contentImageUrl)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(contentImage);  

My aim is to share the image in Imageview. How to get URI on imageview with Glide?

Uri uri = ??????????????

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share Image"));

回答1:


to get Uri change ur Glide implementation to:

Bitmap bitmap;

 Glide.with(getApplicationContext())
                .load(contentImageUrl)
                .asBitmap()
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> 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:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Also for runtime permission: Api>=23

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


来源:https://stackoverflow.com/questions/42200448/how-to-get-uri-on-imageview-with-glide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!