【问题标题】:How to send image directly into the chat using mime type in whatsapp如何使用 whatsapp 中的 mime 类型将图像直接发送到聊天中
【发布时间】:2014-08-19 09:02:27
【问题描述】:

我有一个可以通过 WhatsApp 共享图像的工作应用程序。我使用mime type 将所选图像重定向到 WhatsApp。 但是,当从聊天/WhatsApp 中选择图像附件并选择我的应用程序后,它会从 WhatsApp 中的各种联系人列表重新开始。

我想要的是使用我的应用程序直接在聊天框中分享图像。 这里请理解:

选择附件时

定向到应用主页

随机选择图片并分享到whatsapp

但它会发送给联系人和群组,而不是聊天框:(

这是我的分享代码:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        ImageView image = (ImageView) findViewById(R.id.full_image_view);
        Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();

        File sd = Environment.getExternalStorageDirectory();
        String fileName = "desi.png";
        File dest = new File(sd, fileName);
        try {
            FileOutputStream out;
            out = new FileOutputStream(dest);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        switch (item.getItemId()) {
            case R.id.item:
                Uri uri = Uri.fromFile(dest);
                Intent shareIntent = new Intent();
                shareIntent.setPackage("com.whatsapp");
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
                shareIntent.setType("image/*");
                startActivity(Intent.createChooser(shareIntent,   getResources().getText(R.string.share)));
                return true;

【问题讨论】:

    标签: android android-intent share mime-types


    【解决方案1】:

    您需要返回一个结果。按照以下方式进行(取自android developer trainingthis answer):

    在 switch 语句中更改您的代码:

    case R.id.item:
        Uri uri = Uri.fromFile(dest);
        if (shouldReturnResult()) {
            Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND, uri); //or ACTION_PICK
            this.setResult(Activity.RESULT_OK, shareIntent); //set result
            this.finish(); //exit activity
        } else {
            Intent shareIntent = new Intent();
            shareIntent.setPackage("com.whatsapp");
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
            shareIntent.setType("image/*");
            startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share)));
        }
        return true;
    

    并添加此代码-sn-p:

    private boolean shouldReturnResult() {
        Intent intent = getIntent();
        if (intent!=null && intent.getType()!=null) {
            return intent.getType().indexOf("image/") != -1;
        } else {
            return false;
        }
    }
    

    这样,如果您通过 whatsapp 打开应用,您的应用会返回一张图片,但如果用户以“正常方式”启动,您仍然可以分享图片。

    注意:
    如果您在用户选择图像后立即将结果返回给whatsapp,对用户来说会更好,这样他们就不必再次单击共享图标。

    【讨论】:

    • 活动中的某处,例如onOptionsItemSelected 方法之后。
    • 它说这个方法必须返回一个布尔类型的结果然后...shouldReturnResult 永远不会在本地使用...
    • 上升。代码中有2个错误。我更改了 code-sn-p 的代码(缺少 else)和 switch 语句(在 shouldReturnResult 附近缺少 ())。
    • 好的,但它仍然显示警告:The method shouldReturnResult() from the type FullImageActivity is never used locally
    • 请撤消您的更改并重新复制代码。 shouldReturnResult() 应该来自布尔类型并用于if(shouldReturnResult())
    【解决方案2】:
        Intent shareIntent = new Intent();
        shareIntent.setPackage("com.whatsapp");
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);// uri of perticular picture ( from gallary or camera pic
        shareIntent.setType("image/*");
        startActivity(Intent.createChooser(shareIntent,getResources().getText(R.string.share)));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-08
      相关资源
      最近更新 更多