【问题标题】:Share images to another apps将图像共享到其他应用程序
【发布时间】:2012-09-02 14:33:37
【问题描述】:

在我的应用程序中,我可以获取屏幕截图并将其保存到 SD 卡。我想知道是否可以将它从我的应用程序共享到 Whatsapp,例如,就像画廊应用程序那样。

在图库中,您有机会将文件“分享”到 Whatsapp、Facebook 或 Twitter。

我可以在我的应用中做同样的事情吗?怎么样?

谢谢!

【问题讨论】:

    标签: android image share


    【解决方案1】:

    是的,你可以。

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @SuppressWarnings( "deprecation" )
    public static Intent shareImage(Context context, String pathToImage) {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
            shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        else
            shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
    
        shareIntent.setType("image/*");
    
        // For a file in shared storage.  For data in private storage, use a ContentProvider.
        Uri uri = Uri.fromFile(context.getFileStreamPath(pathToImage));
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        return shareIntent;
    }
    

    这里有详细链接:http://android-developers.blogspot.com/2012/02/share-with-intents.html

    所以只需添加一个“分享”按钮并执行startActivity(shareIntent)

    【讨论】:

      【解决方案2】:

      结合这两个答案,我明白了!谢谢你们!

      private void shareIt() {
          //sharing implementation here
      
          Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
          sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
          sharingIntent.setType("image/*");
      
          Uri uri = Uri.fromFile(file);
          sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
      
          startActivity(Intent.createChooser(sharingIntent, "Share via"));
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-15
        • 2016-02-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多