Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将GIF从imageView共享到Whatsapp_Android_Bitmap_Android Glide_Android Bitmap - Fatal编程技术网

Android 将GIF从imageView共享到Whatsapp

Android 将GIF从imageView共享到Whatsapp,android,bitmap,android-glide,android-bitmap,Android,Bitmap,Android Glide,Android Bitmap,尝试将加载到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方法代码的共享:- Bitm

尝试将加载到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();
位图imgBitmap=drawable.getBitmap();
字符串imgBitmapPath=MediaStore.Images.Media.insertImage(getContentResolver(),imgBitmap,“Whatsapp”,null);
Uri imgUri=Uri.parse(imgBitmapPath);
意向共享意向=新意向(意向.行动\发送);
setFlags(Intent.FLAG\u活动\u新任务);
shareIntent.putExtra(Intent.EXTRA_流,imgUri);
setType(“image/*”);
shareIntent.setPackage(“com.whatsapp”);
addFlags(Intent.FLAG\授予\读取\ URI\权限);
shareIntent.putExtra(Intent.EXTRA_TEXT,“我的自定义文本”);
startActivity(Intent.createChooser(shareIntent,“共享此”);
我的滑行载荷代码:-

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

无法将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);
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()
    }
Glide.with(这个)
.asGif()
.load(“您的\u gif\u url”)
.addListener(对象:RequestListener{
覆盖加载失败(
e:例外情况?,
型号:有吗?,
目标:目标?,
isFirstResource:布尔值
):布尔值{
Log.e(“GIF”、“GIF失败”)
返回错误
}
覆盖资源就绪(
资源:可提取吗?,
型号:有吗?,
目标:目标?,
数据源:数据源?,
isFirstResource:布尔值
):布尔值{
Log.e(“GIF”,“测试GIF完成”)
saveImageAndShare(资源)
返回错误
}
})
.into(img1)
私人乐趣存储图像和共享(可礼品赠送:可礼品赠送?){
有天赋?让我来{
val baseDir:String=Environment.getExternalStorageDirectory().getAbsolutePath()
val fileName=“sharingGif.gif”
val sharingGifFile=文件(baseDir,文件名)
gifDrawable文件(gifDrawable,共享GifFile)
val uri:uri=FileProvider.getUriForFile(
这是BuildConfig.APPLICATION_ID+“.provider”,
共享文件
)
共享文件(uri)
}
}
私有趣味共享文件(uri:uri){
val shareIntent=Intent(Intent.ACTION\u SEND)
shareIntent.type=“image/gif”
shareIntent.putExtra(Intent.EXTRA\u流,uri)
startActivity(Intent.createChooser(shareIntent,“共享表情”))
}
私人趣味gifDrawable文件(gifDrawable:gifDrawable,gifFile:File){
val byteBuffer=giftrawable.buffer
val输出=文件输出流(gifFile)
val bytes=ByteArray(byteBuffer.capacity())
(byteBuffer.duplicate().clear()作为byteBuffer.get(字节)
输出.写入(字节,0,字节.大小)
output.close()
}

giftrawable drawable=(giftrawable)img1.getgiftrawable()?位图不能包含Gif。您知道在对Gif进行uri解析之前,代码会是什么样子吗?我第一次实现GifDrawableThis很好,但是Java中对此有任何参考吗?您好@Abhay,我刚刚通过电子邮件向您发送了此信息。您可以在此处发布您的尝试。我的shareFile方法存储在void onCreate下的单击方法中。在此之前,我将其作为shareFile()放在onclick方法下。如果使用您的代码,我现在如何更改它?您可以继续,因为您只需键入cast即可
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()
    }