使用意图与 Glide 库共享图像 - 图像始终为空

use*_*164 6 android share android-intent android-glide

我有一个运行良好的自定义列表视图,现在我想共享列表中的图像和文本。我已经找到了如何从 SO 执行此操作的步骤,但是当我单击“共享”按钮时,图像始终为空。

使用 Glide 加载图像的图像视图。

if (!Patterns.WEB_URL.matcher(Limage).matches()) {
viewholder.iview.setVisibility(View.GONE);
} else {
Glide.with(convertView.getContext()).load(Limage).centerCrop()
.diskCacheStrategy(DiskCacheStrategy.ALL).listener(new RequestListener<String, GlideDrawable>() {
                           @Override
                           public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                               return false;
                           }

                           @Override
                           public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {

                             //  viewholder.progress.setVisibility(View.GONE);
                               return false;
                           }
                       }).into(viewholder.iview);
            viewholder.iview.setVisibility(View.VISIBLE);
        }
Run Code Online (Sandbox Code Playgroud)

我创建了一个共享按钮,在 onclick 中我正在传递以下代码。

viewholder.share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri bmpUri = getLocalBitmapUri(viewholder.iview);
                if (bmpUri != null) {
                    // Construct a ShareIntent with link to image
                    Intent shareIntent = new Intent();
                    shareIntent.setAction(Intent.ACTION_SEND);
                    shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                    shareIntent.setType("image/*");
                    // Launch sharing dialog for image
                    listdisplay.startActivity(Intent.createChooser(shareIntent, "Share Image"));

                } else {
                    // ...sharing failed, handle error
                }

            }
        });
Run Code Online (Sandbox Code Playgroud)

要从 Imageview 获取图像,我正在使用以下代码。

   private Uri getLocalBitmapUri(ImageView iview) {
        Drawable drawable = iview.getDrawable();
        Bitmap bmp = null;
        if (drawable instanceof BitmapDrawable){

            bmp = ((BitmapDrawable) iview.getDrawable()).getBitmap();

                Log.e("Shiva","Came inside drawable");
        } else {
            Log.e("Shiva","drawable is null"+drawable);
            return null;

        }

        Uri bmpUri = null;

        File file =  new File(listdisplay.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.close();
            bmpUri = Uri.fromFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // **Warning:** This will fail for API >= 24, use a FileProvider as shown below instead.

   return bmpUri;

    }
Run Code Online (Sandbox Code Playgroud)

所以,现在发生的事情是在检查“drawable instanceof BitmapDrawable”的 if 步骤中总是返回 null。这里有什么问题吗?注意:以上代码在适配器内。

Joh*_*ohn 1

使用 Glide 时 iview.getDrawable() 将返回 null。您可以设置:

public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
               viewholder.iview.setDrawable(resource);
               return true;
             }
Run Code Online (Sandbox Code Playgroud)

那么 iview.getDrawable() 将返回可绘制对象