【问题标题】:Sharing GIF from imageView to Whatsapp从 imageView 分享 GIF 到 Whatsapp
【发布时间】:2019-11-29 12:20:31
【问题描述】:

试图将我加载到我的 ImageView 中的 GIF 分享到 Whatsapp。我可以在我的 imageview 中完美地看到 GIF 动画,但是当我尝试分享到 Whatsapp 时会出现此错误:-

java.lang.ClassCastException: com.bumptech.glide.load.resource.gif.GifDrawable cannot be cast to android.graphics.drawable.BitmapDrawable

我分享到 Whatsapp 方法代码:-

BitmapDrawable drawable = (BitmapDrawable) img1.getDrawable();
Bitmap imgBitmap = drawable.getBitmap();
String imgBitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(), imgBitmap, "Whatsapp", null);

Uri imgUri = Uri.parse(imgBitmapPath);

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
shareIntent.setType("image/*");
shareIntent.setPackage("com.whatsapp");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.putExtra(Intent.EXTRA_TEXT, "My Custom Text ");
startActivity(Intent.createChooser(shareIntent, "Share this"));

我的 Glide 加载代码:-

Glide.with(getApplicationContext()).asGif().load(gifUrl).into(img1);

【问题讨论】:

  • GifDrawable drawable = (GifDrawable) img1.getGifDrawable(); ?
  • 位图不能包含 Gif。
  • 知道 uri 解析 gifDrawable 的代码是什么样子的吗?我第一次实现 GifDrawable

标签: android bitmap android-glide android-bitmap


【解决方案1】:

您无法将 GifDrawable 转换为 BitmapDrawable。

从 gifdrawable 中获取 bytebuffer 并使用 bytearray 保存在文件中。然后您可以使用 uri 共享它。检查下面,

Java

Glide.with(this)
         .asGif()
         .load("your_gif_url")
         .addListener(new RequestListener<GifDrawable>() {
               @Override
               public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {

                   return false;
               }

               @Override
               public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
                   saveImageAndShare(resource);
                   return false;
               }
          }).into(img1);

将 gifdrawable 转换为 bytearray 并保存文件,然后使用 uri 共享它

private void saveImageAndShare(GifDrawable gifDrawable) {
    if (gifDrawable != null) {
        String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
        String fileName = "sharingGif.gif";
        File sharingGifFile = new File(baseDir, fileName);
        gifDrawableToFile(gifDrawable, sharingGifFile);
        Uri uri = FileProvider.getUriForFile(this, getPackageName() + ".provider", sharingGifFile);
        this.shareFile(uri);
    }

}

private void shareFile(Uri uri) {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("image/gif");
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(shareIntent, "Share Emoji"));
}

private void gifDrawableToFile(GifDrawable gifDrawable, File gifFile) {
    ByteBuffer byteBuffer = gifDrawable.getBuffer();
    try {
        FileOutputStream output = new FileOutputStream(gifFile);
        byte[] bytes = new byte[byteBuffer.capacity()];
        ((ByteBuffer) byteBuffer.duplicate().clear()).get(bytes);
        output.write(bytes, 0, bytes.length);
        output.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

科特林

Glide.with(this)
                .asGif()
                .load("your_gif_url")
                .addListener(object : RequestListener<GifDrawable> {
                    override fun onLoadFailed(
                        e: GlideException?,
                        model: Any?,
                        target: Target<GifDrawable>?,
                        isFirstResource: Boolean
                    ): Boolean {
                        Log.e("GIF", "GIf failed")
                        return false
                    }

                    override fun onResourceReady(
                        resource: GifDrawable?,
                        model: Any?,
                        target: Target<GifDrawable>?,
                        dataSource: DataSource?,
                        isFirstResource: Boolean
                    ): Boolean {
                        Log.e("GIF", "Test gif done")
                        saveImageAndShare(resource)

                        return false
                    }
                })
                .into(img1)


private fun saveImageAndShare(gifDrawable: GifDrawable?) {

        gifDrawable?.let {

            val baseDir: String = Environment.getExternalStorageDirectory().getAbsolutePath()
            val fileName = "sharingGif.gif"

            val sharingGifFile = File(baseDir, fileName)
            gifDrawableToFile(gifDrawable, sharingGifFile)

            val uri: Uri = FileProvider.getUriForFile(
                this, BuildConfig.APPLICATION_ID + ".provider",
                sharingGifFile
            )

            shareFile(uri)


        }

    }

    private fun shareFile(uri: Uri) {
        val shareIntent = Intent(Intent.ACTION_SEND)
        shareIntent.type = "image/gif"

        shareIntent.putExtra(Intent.EXTRA_STREAM, uri)
        startActivity(Intent.createChooser(shareIntent, "Share Emoji"))
    }

    private fun gifDrawableToFile(gifDrawable: GifDrawable, gifFile: File) {
        val byteBuffer = gifDrawable.buffer
        val output = FileOutputStream(gifFile)
        val bytes = ByteArray(byteBuffer.capacity())
        (byteBuffer.duplicate().clear() as ByteBuffer).get(bytes)
        output.write(bytes, 0, bytes.size)
        output.close()
    }

【讨论】:

  • 这很好,但是 Java 中对此有什么参考吗?
  • 嗨@Abhay,我刚刚给你发了电子邮件。
  • 你可以在这里发布你的尝试。
  • 我的 shareFile 方法存储在 void onCreate 下的 on click 方法中。在此之前,我将其作为 shareFile() 放在 onclick 方法下。如果使用您的代码,我现在如何更改它?
  • 您可以继续进行,只需将类型转换为 GifDrawable
猜你喜欢
  • 1970-01-01
  • 2021-11-14
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 2018-09-04
  • 2017-02-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多