【问题标题】:How to get URI on imageview with Glide如何使用 Glide 在 imageview 上获取 URI
【发布时间】:2017-02-13 09:16:11
【问题描述】:

我正在使用“Glide”将图像从服务器加载到 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"));

【问题讨论】:

  • 您可以从文件路径获取图像 uri。这还不够吗?

标签: android imageview uri


【解决方案1】:

要获取 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);

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

您需要在清单中添加权限:

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

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

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

【讨论】:

  • 发生了这样的错误:java.lang.NullPointerException: uriString at android.net.Uri$StringUri.(Uri.java:480) at android.net.Uri$StringUri.(Uri.java:470) 在 com.uruker.ibrahim.project.ContentDetail.getImageUri(ContentDetail.java:654) 在 com.uruker.ibrahim 的 android.net.Uri.parse(Uri.java:442)。 project.ContentDetail$13$1.onResourceReady(ContentDetail.java:494)
  • 在哪一行?你添加权限了吗?
  • 是添加了权限。另外,其他事件也没有权限错误。
  • 尝试将此行:Uri uri = getImageUri(getApplicationContext(),bitmap); 移出onResourceReady 方法检查编辑
  • 尝试更改此行:String path = MediaStore.Images.Media.insertImage(MainActivity.this.getContentResolver(), inImage, UUID.randomUUID().toString() + ".png", "drawing");
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    • 2016-05-09
    相关资源
    最近更新 更多