将图像共享到其他应用程序

问题描述 投票:2回答:2

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

在图库中,您可以将文件“共享”到Whatsapp,Facebook或Twitter。

我可以通过我的应用程序执行同样的操作吗?怎么样?

谢谢!

android image share
2个回答
3
投票

是,可以。

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


0
投票

结合两个答案,我明白了!感谢大伙们!

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

    }
© www.soinside.com 2019 - 2024. All rights reserved.