Android camera and mail example
Here are some examples of using the Android camera and sending an email:
Camera Example
This example demonstrates how to use the Android camera to take a photo and display it in an ImageView.
Step 1: Add the necessary permissions and features
In your AndroidManifest.xml file, add the following permissions and features:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="true" />
Step 2: Create a CameraActivity
Create a new Java class called CameraActivity
:
public class CameraActivity extends AppCompatActivity {
private Camera camera;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
camera = Camera.open();
imageView = findViewById(R.id.image_view);
}
@Override
protected void onResume() {
super.onResume();
camera.startPreview();
}
@Override
protected void onPause() {
super.onPause();
camera.stopPreview();
camera.release();
}
public void takePicture(View view) {
camera.takePicture(null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
imageView.setImageBitmap(bitmap);
}
});
}
}
Step 3: Add the camera preview to the layout
In your activity's layout file (e.g. activity_camera.xml
), add a SurfaceView to display the camera preview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<SurfaceView
android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Email Example
This example demonstrates how to send an email using the Android Email Intent.
Step 1: Add the necessary permissions
In your AndroidManifest.xml file, add the following permission:
<uses-permission android:name="android.permission.INTERNET" />
Step 2: Create an EmailActivity
Create a new Java class called EmailActivity
:
public class EmailActivity extends AppCompatActivity {
private EditText subjectEditText;
private EditText bodyEditText;
private Button sendButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_email);
subjectEditText = findViewById(R.id.subject_edit_text);
bodyEditText = findViewById(R.id.body_edit_text);
sendButton = findViewById(R.id.send_button);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String subject = subjectEditText.getText().toString();
String body = bodyEditText.getText().toString();
String[] recipients = {"[email protected]"};
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, body);
startActivity(Intent.createChooser(intent, "Send Email"));
}
});
}
}
Step 3: Add the email layout
In your activity's layout file (e.g. activity_email.xml
), add the necessary views:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/subject_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject" />
<EditText
android:id="@+id/body_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Body" />
<Button
android:id="@+id/send_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send" />
</LinearLayout>
That's it! You can now run the app and use the camera to take a photo, and then send an email using the email activity.