【问题标题】:When sharing text and image errors occur in the various versions of Android分享文字和图片时出现各种Android版本错误
【发布时间】:2012-12-16 07:49:12
【问题描述】:

代码如下:

        final Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("*/*");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject.toString());
        intent.putExtra(Intent.EXTRA_TEXT, text.toString());
        intent.putExtra(Intent.EXTRA_STREAM, "file://" + path);

         startActivityForResult(
                Intent.createChooser(intent, title.toString()), REQUEST_CODE);

结果如下

  1. API 10:工作
  2. API 14:错误

【问题讨论】:

  • 我猜你没有在 Galaxy 标签中打开此类文件的应用

标签: java android android-intent share


【解决方案1】:

使用此方法与文字分享照片。调用此方法并传递参数 nameofapp 和 imagepath。应用程序名称表示您想要分享的内容,如 gmail、facebook、twitter。

private void share(String nameApp, String imagePath) {
    try
    {
        List<Intent> targetedShareIntents = new ArrayList<Intent>();
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("image/jpeg");
        List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
        if (!resInfo.isEmpty()){
            for (ResolveInfo info : resInfo) {
                    Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
                    targetedShare.setType("image/jpeg"); // put here your mime type
                    if (info.activityInfo.packageName.toLowerCase().contains(nameApp) || info.activityInfo.name.toLowerCase().contains(nameApp)) {
                        targetedShare.putExtra(Intent.EXTRA_SUBJECT, "Text");
                        targetedShare.putExtra(Intent.EXTRA_TEXT,"This photo is created by APP");
                        targetedShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagePath)) );
                        targetedShare.setPackage(info.activityInfo.packageName);
                        targetedShareIntents.add(targetedShare);
                    }
                }
                Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
                startActivity(chooserIntent);
        }
    }
    catch(Exception e){
    }
 }

像这样使用:-

try {
    File filePath = "/mnt/sdcard/vmphoto.jpg"; //This is imagefile path in your change it acc. to your requirement.
    share("facebook",filePath.toString());

}
catch(Exception e) {
           //exception occur might your app like gmail , facebook etc not installed or not working correctly.
}

【讨论】:

    【解决方案2】:
    final Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("*/*");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject.toString());
        intent.putExtra(Intent.EXTRA_TEXT, text.toString());
        intent.putExtra(Intent.EXTRA_STREAM,
                Uri.fromFile(new File("/" + path)));
    

    【讨论】:

    • 您从上面的帖子中首先看到的那件事,您几天前接受了这个答案,现在写成与上面相同的代码并接受这个答案......
    • 最后一行已更改。在 android 4.0.3 中不工作intent.putExtra(Intent.EXTRA_STREAM, "file://" + path);
    猜你喜欢
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 2014-03-09
    • 2021-01-21
    • 1970-01-01
    相关资源
    最近更新 更多