gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:58:35 24 4
gpt4 key购买 nike

任何帮助,想法我都会采纳。大家好,抱歉这几天问了这么多问题。我设计了简单的 Android 应用程序,它使用相机并显示照片是在布局中拍摄的。看看所附的图片。 enter image description here

一旦我点击“打开相机”按钮,就会弹出相机,要求用户按下相机按钮进行拍摄或拍照。我这里需要的是,一旦相机打开,它应该自行拍照,而无需使用相机按钮,不知道是否可能。

enter image description here

下一个问题如图,需要用户干预,如果用户喜欢就按确定。在此步骤中,我只需要拍摄一张照片,拍摄完成后应返回主要 Activity 。并将图片存储在 Image View 中。 enter image description here

最后一个请求或想法,伙计们如何将这张图片发送到来自用户的电子邮件干扰。这是代码 --------------------------------------------------

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

}

}
<小时/>

最佳答案

首先,您必须将该位图保存到 SD 卡,然后才能将该图像附加到电子邮件中。

将位图保存到SD卡是

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

}

然后您可以阅读该图像并附加到电子邮件

为此,请查看此链接 How to attach Bitmap to email android

关于java - 如何使用Intent拍照并通过电子邮件发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29691826/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com