如何使用 Glide 在 imageview 上获取 URI

How to get URI on imageview with Glide(如何使用 Glide 在 imageview 上获取 URI)
本文介绍了如何使用 Glide 在 imageview 上获取 URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Glide"将图像从服务器加载到 ImageView.我想知道是否可以从图像视图本身中提取该 URI.

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);  

我的目标是在 Imageview 中分享图像.如何使用 Glide 获取 imageview 上的 URI?

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"));

推荐答案

获取 Uri 将你的 Glide 实现更改为:

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;
                    }
                });

现在调用方法;​​

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

getImageUri函数中:

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" />

还有运行时权限:Api>=23

Also for runtime permission: Api>=23

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

这篇关于如何使用 Glide 在 imageview 上获取 URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)