将GIF从imageView分享到Whatsapp



试图将我加载到ImageView中的GIF分享到Whatsapp中。我可以在我的图像视图中看到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.with(getApplicationContext()).asGif().load(gifUrl).into(img1);

您无法将 GifDrawable 转换为 BitmapDrawable。

从 gifdrawable 获取字节缓冲区,并使用文件中的字节数组保存它。 然后您可以使用 URI 共享它。检查下面,

爪哇岛

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 转换为字节数组并保存文件,然后使用 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()
}

最新更新