Android开发:从内部存储中分享动态GIF

3

我想分享我的drawable文件夹中的动画gif图片。 目前代码可以工作,但共享的gif文件没有动画。你只能看到动画的第一张图片。有人知道如何让它工作吗?

Bitmap icon = BitmapFactory.decodeResource(this.getResources(),
            R.drawable.animated_gif);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/gif");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();

icon.compress(Bitmap.CompressFormat.PNG, 100, bytes);

File f = new File(Environment.getExternalStorageDirectory()
        + File.separator + "temporary_file.gif");
try {
    f.createNewFile();
    FileOutputStream fo = new FileOutputStream(f, true);
    fo.write(bytes.toByteArray());
} catch (IOException e) {
    e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM,
        Uri.parse("file:///sdcard/temporary_file.gif"));
startActivity(Intent.createChooser(share, "Share Image"));
1个回答

0

嗯,你正在从drawable获取静态位图。我建议你使用Glide库中的GifDrawable,并采用以下方法发送动画gif(如果你将gif图像加载到ImageView中):

private Uri getLocalBitmapUri(ImageView imageView, String link) {
    // Extract Bitmap from ImageView drawable
    Drawable drawable = imageView.getDrawable();
    if (drawable instanceof GifDrawable) {
        try {
            // Store image to default external storage directory
            String fileName = link.substring(link.lastIndexOf('/') + 1, link.length());
            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "shared_gif_" + System.currentTimeMillis() + ".gif");
            file.getParentFile().mkdirs();
            GifDrawable gifDrawable = ((GifDrawable) imageView.getDrawable());
            FileOutputStream out = new FileOutputStream(file);
            out.write(gifDrawable.getData());
            out.close();
            return Uri.fromFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

...
Uri bmpUri = Utils.getLocalBitmapUri(gifImageView, post.media_content.get(0).file);
if (bmpUri != null) {
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("image/gif");
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "title");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "text");
    sharingIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
    startActivity(Intent.createChooser(sharingIntent, "Share via"));
} else {
    // ...sharing failed, handle error
}
...

gifDrawable.getData()?没有这个方法,我该如何获取数据? - Hadas Kaminsky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接