【问题标题】:send a image from gallery in email service in android在 android 的电子邮件服务中从图库发送图像
【发布时间】:2017-04-30 07:27:37
【问题描述】:

我想在我的 Android 应用中通过电子邮件发送图像。为此,我正在使用 Android Native Camera 应用程序和 Intents 来使用相应的服务。我使用了以下代码:
电子邮件正在发送,但如果我尝试添加图像,应用程序会崩溃。

public class Complaints extends AppCompatActivity {
    Button sendEmail;
    EditText to, subject, msg;
    Bitmap image;
    Button camera;
    File pic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complaints);
        to = (EditText) findViewById(R.id.et1);
        subject = (EditText) findViewById(R.id.et2);
        msg = (EditText) findViewById(R.id.et3);
        sendEmail = (Button) findViewById(R.id.s_Email);
        camera = (Button) findViewById(R.id.btn_img);
        camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(Intent.createChooser(intent,"Select Picture"));
            }
        });


        sendEmail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String Emailid = to.getText().toString();
                String sub = subject.getText().toString();
                String message = msg.getText().toString();

                Intent email = new Intent(Intent.ACTION_SEND);
                email.putExtra(Intent.EXTRA_EMAIL, new String[]{Emailid});
                email.putExtra(Intent.EXTRA_SUBJECT, sub);
                email.putExtra(Intent.EXTRA_TEXT, message);
                email.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//this will make such that when user returns to your app, your app is displayed, instead of the email app.
               email.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));
               email.setType("message/rfc822");
             email.setType("image/jpeg");

                try {
                    startActivity(Intent.createChooser(email, "Message was Sent"));
                }
                catch (ActivityNotFoundException e) {
                    Toast t = Toast.makeText(Complaints.this, "There is No Emial Client installed ", Toast.LENGTH_SHORT);
                    t.setGravity(Gravity.CENTER, 0, 10);
                    t.show();
                }
            }
        });


    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10) {
            image = (Bitmap) data.getExtras().get("Data");
            ImageView i = (ImageView) findViewById(R.id.img);
            i.setImageBitmap(image);
            try
            {
                File root= Environment.getExternalStorageDirectory();
                if(root.canWrite())
                {
                    pic=new File(root,"pic.jpeg");
                    FileOutputStream out=new FileOutputStream(pic);
                    image.compress(Bitmap.CompressFormat.JPEG,100,out);
                    out.flush();
                    out.close();
                }

            } catch (IOException e)
            {
                Log.e("BROKEN", "Could not write file " + e.getMessage());
            }
        }
    }

}

【问题讨论】:

  • 错误信息是什么?您是否在清单文件中添加了 ?对于这一行“文件根= Environment.getExternalStorageDirectory()”
  • java.lang.RuntimeException: 传递结果失败 ResultInfo{who=null, request=10, result=-1, data=Intent { dat=content://com.android.providers.media.文档/文档/图像:18738 flg=0x1 }} 到活动 {com.example.ankit.haridwartrafficpolice/com.example.ankit.haridwartrafficpolice.Complaints}:java.lang.NullPointerException:尝试调用虚拟方法'java.lang。空对象引用上的对象 android.os.Bundle.get(java.lang.String)'
  • 引起:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.Object android.os.Bundle.get(java.lang.String)'跨度>
  • 错误是由这一行产生的:image = (Bitmap) data.getExtras().get("Data");您需要使用: Uri dataUri = data.getData();来源:developer.android.com/training/basics/intents/result.html

标签: android android-intent email-attachments android-gallery mediastore


【解决方案1】:
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;


import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;

import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.Media;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;


 public class Complaints extends Activity {
  Button send;
  Bitmap thumbnail;
  File pic;
  EditText address, subject, emailtext;
  protected static final int CAMERA_PIC_REQUEST = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_complaints);
    send=(Button) findViewById(R.id.emailsendbutton);
    address=(EditText) findViewById(R.id.emailaddress);
    subject=(EditText) findViewById(R.id.emailsubject);
    emailtext=(EditText) findViewById(R.id.emailtext);










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

        @Override
        public void onClick(View arg0){
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);  

        }
        });

        send.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0){

            Intent i = new Intent(Intent.ACTION_SEND);
            i.putExtra(Intent.EXTRA_EMAIL, new String[]{"dummy@email.com"});
            i.putExtra(Intent.EXTRA_SUBJECT,"dummy subject");
            //Log.d("URI@!@#!#!@##!", Uri.fromFile(pic).toString() + "   " + pic.exists());
            i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));

            i.setType("image/png");
            startActivity(Intent.createChooser(i,"Share this"));
        }
        });


} 

【讨论】:

    【解决方案2】:

    获取图像路径并将路径转换为 ​​Uri :

    File photo = new File(Environment.getExternalStorageDirectory()+"/Android/data/"+getApplicationContext().getPackageName()+"/Fault", imagename+".png")
    Uri imageuri = Uri.fromFile(photo); 
    

    通过电子邮件意图发送:

    Intent send_report = new Intent(Intent.ACTION_SEND);
                                        send_report.putExtra(Intent.EXTRA_EMAIL, new String[]{ email_emailid}); 
                                        send_report.putExtra(Intent.EXTRA_SUBJECT, email_subject);
                                        send_report.putExtra(Intent.EXTRA_STREAM, imageuri);
                                        send_report.putExtra(Intent.EXTRA_TEXT, email_body);  
                                        send_report.setType("text/plain");
                                        send_report.setType("image/png");
                                        startActivityForResult(Intent.createChooser(send_report, "Choose an Email client"), 77);
    

    【讨论】:

    • ava.lang.RuntimeException: 传递结果失败 ResultInfo{who=null, request=10, result=-1, data=Intent { dat=content://com.android.providers.media.文档/文档/图像:18738 flg=0x1 }} 到活动 {com.example.ankit.haridwartrafficpolice/com.example.ankit.haridwartrafficpolice.Complaints}:java.lang.NullPointerException:尝试调用虚拟方法'java.lang。 Object android.os.Bundle.get(java.lang.String)' 在一个空对象上引用
    • 引起:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.Object android.os.Bundle.get(java.lang.String)'跨度>
    猜你喜欢
    • 2012-04-24
    • 2015-08-01
    • 1970-01-01
    • 2015-11-27
    • 2021-08-22
    • 2013-04-20
    • 2013-08-23
    相关资源
    最近更新 更多