试图共享我加载到ImageView中的Whatsapp的GIF。我可以在我的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);
答案 0 :(得分:1)
您不能将GifDrawable转换为BitmapDrawable。
从gifdrawable获取字节缓冲区,并使用字节数组将其保存在文件中。那么您可以使用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转换为字节数组并保存文件,然后使用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()
}