android Share Intent 不适用于文本和图像 [英] android Share Intent not works for text and image both

查看:72
本文介绍了android Share Intent 不适用于文本和图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi there is the code i used to share text and image :

    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 是:

App is my custom class and App.getString is :

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

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

it don't works well on Telegram, Gmail and same Apps And only share text with out Image

推荐答案

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

i found the answer for it, it work for me and i sent text and image both in one message to Telegram app.here is my code.

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

这篇关于android Share Intent 不适用于文本和图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆