【问题标题】:android Share Intent not works for text and image bothandroid Share Intent 不适用于文本和图像
【发布时间】:2015-12-01 10:31:28
【问题描述】:

您好,这是我用来分享文字和图片的代码:

    Toast.makeText(App.getContext(), "Share", Toast.LENGTH_LONG).show();
    Intent sharingIntent = null;
    if(!image_resource.isEmpty()){
        sharingIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
        String imagePath = "SD-Card Path";
        ExceptionHelpers.dLog("SHARE_LOG", "Share image path : " + imagePath);
        ExceptionHelpers.dLog("SHARE_LOG", "Share image exist : " + (new File(imagePath).exists())); // It return 'true' on LogCat
        if(new File(imagePath).exists()) {
            ExceptionHelpers.dLog("SHARE_LOG", "Share image and text");
            Uri imageUri = Uri.fromFile(new File(imagePath));
            sharingIntent.setType("*/*"); // also 'image/*' tested and not works
            sharingIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
        }else{
            ExceptionHelpers.dLog("SHARE_LOG", "Share text");
            sharingIntent = new Intent(Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
        }
    }else {
        ExceptionHelpers.dLog("SHARE_LOG", "Share text");
        sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
    }
    String shareBody = ""+App.getString(R.string.this_text_shared_from)+" : "+App.getString(R.string.app_name)+" "+App.getString(R.string.share_text_2)+" \n "+
            App.getString(R.string.share_about);
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "" + App.getString(R.string.app_name));
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
    startActivity(Intent.createChooser(sharingIntent, "" + App.getString(R.string.share_via)));

App 是我的自定义类,而 App.getString 是:

    public static String getString(int resId){
        return App.context.getString(resId);
    }

它在 Telegram、Gmail 和相同的应用程序上效果不佳,并且只能在没有图像的情况下共享文本

【问题讨论】:

  • 请解释“不起作用”和“不起作用”的含义。请注意,imageUri 在您的putExtra() 调用中始终为null,因为您已两次声明Uri imageUri
  • 它适用于 WhatsApp,但在 Telegram 上:“不支持的内容”显示
  • 使用 android.content.Intent.EXTRA_SUBJECT 注释该行,并在sharingIntent 对话框的正文或标题中传递应用名称,SUBJECT 用于处理电子邮件的应用

标签: android share


【解决方案1】:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
        sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    else
        sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);

    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
    sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
    sharingIntent.setType("image/*");
    sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    sharingIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    getActivity().startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.share_using)));

【讨论】:

    【解决方案2】:

    我找到了答案,它对我有用,我在一条消息中将文本和图像都发送到 Telegram 应用程序。这是我的代码。

    Intent intent = new Intent(Intent.ACTION_SEND);
    packageName="org.telegram.messenger";
                intent.setPackage(packageName);
    //this is my art.... 
    intent.setType("image/text/plain");
    
    
    path=getBitMapPath();//this is path of image 
    uri = Uri.parse(path);
    putExtra(Intent.EXTRA_STREAM, uri);
    putExtra(Intent.EXTRA_TEXT, "hello telegram. i do it!");
    ss = Intent.createChooser(intent, "hamid");
    addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    //addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
    startActivity(ss);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-05
      • 1970-01-01
      • 2012-01-07
      • 2015-03-30
      相关资源
      最近更新 更多