Wolfiebae
Wolfiebae

Reputation: 395

Sharing GIF from imageView to Whatsapp

Trying to share a GIF i loaded into my ImageView into Whatsapp. I can see the GIF animating in my imageview perfectly fine, but it will get this error when I try to share to Whatsapp:-

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

My sharing to Whatsapp method code :-

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

My Glide load code:-

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

Upvotes: 0

Views: 414

Answers (1)

Abhay Koradiya
Abhay Koradiya

Reputation: 2117

You can't convert GifDrawable to BitmapDrawable.

Get bytebuffer from gifdrawable and save it using bytearray in file. then you can share it using uri. Check below,

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

convert gifdrawable to bytearray and save it file then share it using 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();
    }
}

Kotlin

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

Upvotes: 1

Related Questions