从 imageView 分享 GIF 到 Whatsapp
Sharing GIF from imageView to 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加载代码:-
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);
将 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();
}
}
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()
}
尝试将我加载到 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加载代码:-
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);
将 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();
}
}
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()
}