如何使用whatsapp中的mime类型将图像直接发送到聊天中

时间:2014-06-28 09:50:35

标签: android android-intent share mime-types

我有一个工作应用程序,通过WhatsApp共享图像。我使用mime type将所选图像重定向到WhatsApp。 但是,在选择我的应用程序后从chat / WhatsAppand中选择了图像附件时,它会再次显示WhatsApp中各种联系人的列表。

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

when attachment selected.

选择附件时

directed to apps main page

指向应用主页

random image selected and shared to whatsapp

选择随机图像并与whatsapp共享

but it sends to contacts and groups instead the chat box

但它会向联系人和群组发送聊天框:(

这是我的共享代码:

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

2 个答案:

答案 0 :(得分:1)

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

在switch-statement中更改代码:

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;

并添加此代码段:

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

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

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

答案 1 :(得分:-1)

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