如何使用Glide在imageview上获取URI

时间:2017-02-13 09:16:12

标签: android imageview uri

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

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

........

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

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

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

2 个答案:

答案 0 :(得分:4)

让Uri将您的Glide实施更改为:

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);
<{1>}函数中的

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

另外,对于运行时权限:Api&gt; = 23

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

答案 1 :(得分:0)

addNewPostImage 是图片视图; 图片是用 Glide 库插入的

Imageview addNewPostImage = findviewbyid(R.id.addNewPostImage);    
Bitmap bitmap;
EditText addNewPostWebLink = findviewbyid(R.id.addNewPostWebLink);
 
   private void getUri() {
    String imageUrl = addNewPostWebLink.getText().toString().trim();
    Glide.with(getApplicationContext())
            .asBitmap()
            .load(imageUrl)
            //.fitCenter()
            .into(new CustomTarget<Bitmap>() {
                @Override
                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                    // you can do something with loaded bitmap here
                    //bitmap = resource;
                    addNewPostImage.setImageBitmap(resource);
                    }

                @Override
                public void onLoadCleared(@Nullable Drawable placeholder) {

                }
            });
    postImageUri = getImageUri(NewPostActivity.this);
    Toast.makeText(this, Objects.requireNonNull(postImageUri).toString(), Toast.LENGTH_SHORT).show();
}

由于图像位图已在资源主体上设置,我们调用 getImageUri(),它首先从图像中获取位图,对其进行压缩并存储图像。存储后,它返回存储的路径。图像存储在外部存储中

private Uri getImageUri(NewPostActivity newPostActivity) {
    bitmap = ((BitmapDrawable) addNewPostImage.getDrawable()).getBitmap();
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(newPostActivity.getContentResolver(), bitmap, UUID.randomUUID().toString() + ".png", "drawing");
    return Uri.parse(path);
}