如何使用意向共享将gif图像共享到可用的应用程序?

问题描述:

我想与像whatsapp这样的可用应用程序共享.gif,但无法在我的可绘制资源中获得有效的UIF。如何使用意向共享将gif图像共享到可用的应用程序?

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); 

有很多的解决方案上共享图像,但在分享GIF无解,请帮忙

我发现我的答案, 对于GIF我只需要创建一个文件,扩展名为.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")); 
+0

如果什么GIF是在服务器上? –

+0

@ButaniVijay:本地下载gif,然后分享相同的代码:) –

+0

干得好。保存我的一天:) –

if you want to take image from internal storage. 

//this is method having internal storage path. 
private String getFilePath2() { 
    String root = Environment.getExternalStorageDirectory().toString(); 
    File myDir = new File(root + "/Gifs temp/"); 
    if (!myDir.exists()) 
     myDir.mkdirs(); 
    String fname = "temp_gif"; 
    SimpleDateFormat timeStampFormat = new SimpleDateFormat(
      "yyyyMMdd_HHmmss"); 
    Date myDate = new Date(); 
    String filename = timeStampFormat.format(myDate); 
    File file = new File(myDir, fname + "_" + filename + ".gif"); 
    if (file.exists()) { 
     file.delete(); 
    } 
    String str = file.getAbsolutePath(); 
    return str; 
} 




// Then take image from internal storage into the string. 
    String st = getFilePath2(); 




//And share this by this intent 
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"));