简体   繁体   English

如何使用Intent拍照并通过电子邮件发送

[英]How to take picture using Intent and send it by email

Any help , Ideas I will be appropriated . 任何帮助,我都会被采纳。 Hello Guys, sorry am asking so much question these days. 大家好,最近很抱歉问了这么多问题。 I designed simple android app that use the camera and show the picture is taken at the layout. 我设计了一个简单的android应用,该应用使用相机并显示图片是在布局上拍摄的。 Look at the attached pictures. 查看所附图片。 在此处输入图片说明

Once I click the "open the Camera " button it will pop up the camera asking the use to press the camera buttom to shot or take picture. 单击“打开相机”按钮后,它将弹出相机,要求用户按下相机按钮进行拍摄或拍照。 What I need here , once the camera opens, it should take picture by it self with out use the camera button , Idn if is that possible or not. 我在这里需要的是,一旦相机打开,它应该自行使用相机按钮拍照,如果可能的话。

在此处输入图片说明

The next problem as shown in the picture, it requires user interference to press ok if the user like it . 如图所示的下一个问题,如果用户喜欢,则需要用户干预才能按OK。 here in this step I need to take just one picture once is taken should go back to main activity. 在此步骤中,我只需要拍摄一张照片,一旦拍摄应回到主要活动。 and store the picture in Image View . 并将图片存储在Image View中。 在此处输入图片说明

the last request or ideas guys how to send this pict to the email interference from the user . 最后的请求或想法的人如何将这个图片发送给用户的电子邮件干扰。 here is the Code ----------------------------------- 这是代码-----------------------------------

public class MainActivity extends Activity {
    ImageView viewpict;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewpict=(ImageView) findViewById(R.id.pict_result);
        Button btn= (Button)findViewById(R.id.camera);

        btn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent (android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            //  Intent intent = new Intent (getApplicationContext(),MainActivity2.class);

                //startActivity(intent);
                startActivityForResult(intent,0);

            }

     });


    }

protected void onActivityResult( int requestCode, int resultCode,Intent data)
{
    if (requestCode==0)
    {
        Bitmap theimage = (Bitmap) data.getExtras().get("data");
        viewpict.setImageBitmap(theimage);
    }

}

}

First you have to save that bitmap to sdcard then you can attach that image to email. 首先,您必须将该位图保存到sdcard,然后可以将该图像附加到电子邮件。

to save bitmap to sdcard is 将位图保存到sdcard是

FileOutputStream out = null;
 try {
out = new FileOutputStream(filename);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap    instance
// PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
e.printStackTrace();
} 
   try {
    if (out != null) {
        out.close();
    }
} catch (IOException e) {
    e.printStackTrace();

}

and then you can read that image and attach to email 然后您可以阅读该图像并附加到电子邮件中

for that have a look at this link How to attach Bitmap to email android 为此,请查看此链接如何将位图附加到电子邮件android

To send the image through mail, you can use this: 要通过邮件发送图像,可以使用以下方法:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
emailIntent.setType("application/image");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, "Test Email"); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Test Subject"); 
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "From My App"); 
emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("file:///mnt/sdcard/Myimage.jpeg"));
startActivity(Intent.createChooser(emailIntent, "Send mail..."));

In your onActivityResult method you should open a new Activity to view the image preview, one that has an xml with an imageview, ok and cancel buttons. 在您的onActivityResult方法中,您应该打开一个新的Activity以查看图像预览,其中包含一个带有imageview,确定和取消按钮的xml。

Send your bitmap to this activity and put it in its imageview - the preview activity can have a static bitmap ImagePreview property. 将位图发送到此活动并将其放入其imageview-预览活动可以具有静态位图ImagePreview属性。

When pressing ok, you should save image to sdcard, and then send email, as described by @Moubeen. 按OK时,应将图像保存到sdcard,然后按照@Moubeen的说明发送电子邮件。

you can convert a bitmap into PNG in external storage. 您可以在外部存储中将位图转换为PNG。

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File imageFile = new File(path, getCurrentTime()+ ".png");
FileOutputStream fileOutPutStream = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 80, fileOutPutStream);

fileOutPutStream.flush();
fileOutPutStream.close();  

Then, you will get URI through url.Parse(). 然后,您将通过url.Parse()获得URI。 which you can share . 您可以分享。

Uri.parse("file://" + imageFile.getAbsolutePath());  


Intent intent= new Intent(android.content.Intent.ACTION_SEND); 
intent.setType("application/image");
intent.putExtra(android.content.Intent.EXTRA_EMAIL, "Email"); 
intent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Your Subject"); 
intent.putExtra(android.content.Intent.EXTRA_TEXT, "Text"); 
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + imageFile.getAbsolutePath()));
startActivity(Intent.createChooser(intent, "mail sending"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM