【问题标题】:how to share gif images using intent sharing to available apps?如何使用意图共享将 gif 图像共享到可用应用程序?
【发布时间】:2017-01-29 08:36:43
【问题描述】:

我想与 whatsapp 等可用应用共享 .gif,但无法在我的可绘制资源中获取有效的 gif Uri。

 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没有解决方案,请帮忙

【问题讨论】:

    标签: android android-intent gif sharing


    【解决方案1】:

    我找到了答案,我刚刚创建了一个扩展名为 .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"));
    }
    

    【讨论】:

    • 如果 gif 在服务器上怎么办?
    • @ButaniVijay :在本地下载 gif,然后使用相同的代码共享 :)
    • 如果我的 gif 文件在可绘制文件夹中,那么我应该在资源名称中传递什么?
    • @ButaniVijay :我使用了您的代码但失败了。我的图像在服务器上,我将其下载到存储中,然后获取确切路径,但我的应用程序仍然崩溃,并且 logcat 中没有错误消息来消除错误。
    • 此代码无效。仅共享静止图像而不共享动画 gif。
    【解决方案2】:
    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"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 2016-06-04
      相关资源
      最近更新 更多