如何将应用程序中的图像附加到Gmail

时间:2014-07-25 02:18:46

标签: android gmail attachment

该应用程序在屏幕上有2个按钮,1个用于拍摄照片并将其附加在屏幕上,第二个用于将照片附加到gmail&屏幕上。把它发给别人。我将此代码用于第二个按钮

public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
            "mailto", "test@gmail.com", null));
    intent.putExtra(Intent.EXTRA_SUBJECT, "test");
    intent.putExtra(Intent.EXTRA_TEXT, "text");
    intent.putExtra(Intent.EXTRA_STREAM, imageUri);
    startActivity(Intent.createChooser(intent, "Choose an Email client :"));
}

但它没有在gmail上附上照片。这可能是错的。这是代码的其余部分。

public class MainActivity extends Activity {

    private static int TAKE_PICTURE = 1;
    private Uri imageUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button sendbutton = (Button) findViewById(R.id.sendbutton);
        sendbutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                        "mailto", "test@gmail.com", null));
                intent.putExtra(Intent.EXTRA_SUBJECT, "test");
                intent.putExtra(Intent.EXTRA_TEXT, "text");
                intent.putExtra(Intent.EXTRA_STREAM, imageUri);
                startActivity(Intent.createChooser(intent,
                        "Choose an Email client :"));
            }
        });

        Button cameraButton = (Button) findViewById(R.id.button_camera);
        cameraButton.setOnClickListener(cameraListener);
    }

    private OnClickListener cameraListener = new OnClickListener() {
        public void onClick(View v) {
            takePhoto(v);
        }
    };

    public void takePhoto(View v) {
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        File photo = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                "pic.jpg");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
        imageUri = Uri.fromFile(photo);
        startActivityForResult(intent, TAKE_PICTURE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case 1:
            if (resultCode == Activity.RESULT_OK) {
                Uri selectedImage = imageUri;
                getContentResolver().notifyChange(selectedImage, null);
                ImageView imageView = (ImageView) findViewById(R.id.image_view_camera);
                ContentResolver cr = getContentResolver();
                Bitmap bitmap;
                try {
                    bitmap = android.provider.MediaStore.Images.Media
                            .getBitmap(cr, selectedImage);
                    imageView.setImageBitmap(bitmap);
                    Toast.makeText(MainActivity.this, selectedImage.toString(),
                            Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    Toast.makeText(MainActivity.this, "failed to load",
                            Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个

您可以在此链接中查看更多内容:Sharing content in android using action send intent

 public void onClick(View v) {
    // TODO Auto-generated method stub
            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            sharingIntent.setType("image/png");
            sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "test");
            sharingIntent.putExtra(Intent.EXTRA_TEXT, "text");                        
            sharingIntent.putExtra(Intent.EXTRA_STREAM, targetUri);
            startActivity(Intent.createChooser(sharingIntent, "Share image using"));
}