java - 我需要通过邮件从相机发送图像

标签 java android email android-camera

我对 android studio 和东西有点陌生...所以我被卡住了。我需要捕获图片并将其发送到电子邮件。`public class MainActivity extends Activity implements OnClickListener {

EditText editTextEmail, editTextSubject, editTextMessage;
Button btnSend, btnAttachment;
String email, subject, message, attachmentFile;
Uri URI = null;
private static final int PICK_FROM_GALLERY = 101;
int columnIndex;
protected static final int CAMERA_PIC_REQUEST = 0;
Bitmap thumbnail;
File pic;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editTextEmail = (EditText) findViewById(R.id.editTextTo);
    editTextSubject = (EditText) findViewById(R.id.editTextSubject);
    editTextMessage = (EditText) findViewById(R.id.editTextMessage);
    btnAttachment = (Button) findViewById(R.id.buttonAttachment);
    btnSend = (Button) findViewById(R.id.buttonSend);

    btnSend.setOnClickListener(this);
    btnAttachment.setOnClickListener(this);
}





@Override
public void onClick(View v) {

    if (v == btnAttachment) {
        openGallery();

    }
    if (v == btnSend) {
        try {
            email = editTextEmail.getText().toString();
            subject = editTextSubject.getText().toString();
            message = editTextMessage.getText().toString();

            final Intent emailIntent = new Intent (Intent.ACTION_SEND);
           // emailIntent.setType("message/rfc822");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                    new String[] { email });
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                    subject);
            if (URI != null) {
                emailIntent.putExtra(Intent.EXTRA_STREAM, URI.fromFile(pic));
            }

            emailIntent.setType("image/png");
            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
            this.startActivity(Intent.createChooser(emailIntent,
                    "Sending email..."));

        } catch (Throwable t) {
            Toast.makeText(this,
                    "Request failed try again: " + t.toString(),
                    Toast.LENGTH_LONG).show();
        }
    }

}

public void openGallery(){
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST) {
        thumbnail = (Bitmap) data.getExtras().get("data");
        ImageView image = (ImageView) findViewById(R.id.imageView1);
        image.setImageBitmap(thumbnail);


        try {
            File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            if (root.canWrite()){
                pic = new File(root, "pic.png");
                FileOutputStream out = new FileOutputStream(pic);
                thumbnail.compress(Bitmap.CompressFormat.PNG, 100, out);
                out.flush();
                out.close();
            }
        } catch (IOException e) {
            Log.e("BROKEN", "Could not write file " + e.getMessage());
        }

    }
}
}

这是我的代码,我可以用它拍照或发送没有照片的电子邮件。

附言我发现自己的错误。 list 文件中哪里没有权限

最佳答案

使用 Intent 调用您的邮件应用程序并附加图像:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
emailIntent.setType("application/image");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{strEmail}); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Add your Subject Here"); 
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Hey, I send you this Image from my Camera App"); 
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("Your ImagePath"));
startActivity(Intent.createChooser(emailIntent, "Send mail..."));

关于java - 我需要通过邮件从相机发送图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37220605/

相关文章:

java - 具有本地内部类的 JUnit 4 测试方法在测试用例之间不一致

java - 无法使用不同的用户 ID 登录我的 Facebook 与 Android 应用程序集成

Python O365 发送邮件

html - MailChimp HTML 电子邮件模板扩展表格行的部分背景

python - 从 python 作为 google 组发送电子邮件

带有要在 Talend 中实现的参数的 Java 程序

java - 如何在 camunda 中建模异步服务任务并在 Spring Boot 中实现

java - 将 JMSListener 与 RabbitMQ 结合使用

android - 对话框中是否可以有动画列表?

android - 在 Android 应用程序中获取连接的 HDMI 设备的 HDMI 版本