Android:如何在共享期间跳过选择器对话框

时间:2015-12-19 08:36:32

标签: android android-intent

我想从自定义键盘发送一个图像文件。我想跳过选择器刻录并在我的自定义键盘共享期间发送照片。

以下是我的代码

String root_sd = Environment.getExternalStorageDirectory()。toString();

            File imgFile = new  File(root_sd + "/My Photo/"+"Image-6260.jpg");

            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);

            sharingIntent.setType("image/png");

            sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imgFile));
            startActivity(Intent.createChooser(sharingIntent, "Share via"));
            startActivity(sharingIntent);

enter image description here

1 个答案:

答案 0 :(得分:1)

  

您可以通过使用包管理器共享图像/文本,请在发送之前检查watsapp是否已安装,我在下面的例子中没有这样做..

PackageManager pm = getPackageManager();
        try {
            Intent waIntent = new Intent(Intent.ACTION_SEND);
            waIntent.setType("text/plain");
            String text = "YOUR TEXT HERE";
            File imgFile = new File(Environment.getExternalStorageDirectory()
                    + "/DCIM/CandyCam/" + "ktk.jpg");
            PackageInfo info = pm.getPackageInfo("com.whatsapp",
                    PackageManager.GET_META_DATA);
            // Check if package exists or not. If not then code
            // in catch block will be called
            waIntent.setPackage("com.whatsapp");
            if (imgFile.exists()) {
                // waIntent.putExtra(Intent.EXTRA_TEXT, text);
                waIntent.setType("image/jpeg");
                waIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imgFile));
            }
            startActivity(Intent.createChooser(waIntent, "Share with"));

        } catch (NameNotFoundException e) {
            Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
                    .show();
        }