如何使用意图共享分享GIF图像到可用的应用程序?

Ele*_*tro 5 android sharing gif android-intent

我想与whatsapp等可用的应用程序共享.gif,但无法在我的drawable资源中获得有效的Uri gri.

 Uri path = Uri.parse("android.resource://my_package_name/" + R.drawable.gif_1);
    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("image/gif");
    shareIntent.putExtra(Intent.EXTRA_STREAM, path);
Run Code Online (Sandbox Code Playgroud)

有很多关于共享图像的解决方案,但没有关于gif共享的解决方案,请帮忙

Ele*_*tro 12

我找到了答案,我刚刚创建了一个扩展名为.gif的文件,它对我有用.请参阅以下代码:

private void shareGif(String resourceName){

    String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
    String fileName = "sharingGif.gif";

    File sharingGifFile = new File(baseDir, fileName);

    try {
        byte[] readData = new byte[1024*500];
        InputStream fis = getResources().openRawResource(getResources().getIdentifier(resourceName, "drawable", getPackageName()));

        FileOutputStream fos = new FileOutputStream(sharingGifFile);
        int i = fis.read(readData);

        while (i != -1) {
            fos.write(readData, 0, i);
            i = fis.read(readData);
        }

        fos.close();
    } catch (IOException io) {
    }
    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("image/gif");
    Uri uri = Uri.fromFile(sharingGifFile);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(shareIntent, "Share Emoji"));
}
Run Code Online (Sandbox Code Playgroud)