Camera App in Android Studio (Java)
Build a simple camera app: take a photo with the phone's camera app, show it on screen and share it. You will learn Intents and FileProvider.
What you'll learn
- Open the camera with an Intent
- Receive the photo back in your app
- Save it safely with FileProvider
- Share the photo to other apps
Before you start
- Android Studio (any recent version)
- Basic Java: variables, methods and if/else
- An Android phone with a camera
How it fits together
- 1Take photo button
- 2FileProvider address
- 3Camera app saves full photo
- 4ImageView shows it
- 5Share
Build it step by step
1. Create the project
In Android Studio choose New Project โ Empty Views Activity, with Language Java and Minimum SDK API 24.
2. Design the screen
In activity_main.xml, add a rounded card holding an ImageView (with a "Your photo will appear here" hint), a Take photo button and a Share button that starts disabled.
3. Give the camera somewhere to save
The camera app can't write into your app's private files directly. A FileProvider gives it a safe content:// address for one file:
- Create
res/xml/provider_paths.xml, allowing theimages/folder in your cache. - Register the
FileProviderinside<application>in the manifest, with the authority${applicationId}.provider.
4. Ask for a full-size photo
Register ActivityResultContracts.TakePicture(). When you launch it with your FileProvider address, the camera saves the full-resolution photo straight into your file. The callback gets true if a photo was taken and false if the user backed out.
5. Show the photo
In the callback, call imageView.setImageURI(photoUri), hide the hint and enable Share.
6. Share it
Send an ACTION_SEND intent with the photo as EXTRA_STREAM, and add FLAG_GRANT_READ_URI_PERMISSION so the chosen app can read it.
7. Run it
Run on a phone, tap Take photo, snap a picture, then Share it to any app. Most emulators also have a virtual camera.
If something goes wrong
- Crash with "Failed to find configured root": the folder in
provider_paths.xmlmust match the folder you create in code (images/). - The photo doesn't change on the second try: set the image to
nullfirst. The address stays the same, so theImageViewthinks nothing changed. - "No camera app found": the emulator or device has no camera app installed.
What's improved over the video
The video showed the small thumbnail the camera returns; this saves the full-resolution photo. Backing out no longer crashes the app, and you can share the photo.
Files affected
Check what to add and what to replace before copying code.
Application code
Select a file to see its code and where it belongs.
This code uses package com.example.cameraapp. Change that first line to your own package, the one at the top of your MainActivity.
app/src/main/java/com/example/cameraapp/MainActivity.javaOpens the camera app, shows the full photo, and shares it.
package com.example.cameraapp;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import java.io.File;
/**
* Takes a full-resolution photo with the phone's camera app, shows it, and shares it.
* The camera app writes straight into a file we own, shared safely through FileProvider.
*/
public class MainActivity extends AppCompatActivity {
Button btnpicture, btnShare;
ImageView imageView;
TextView placeholder;
private Uri photoUri;
// TakePicture saves the full photo to photoUri and reports true when a photo was taken.
private final ActivityResultLauncher<Uri> takePicture =
registerForActivityResult(new ActivityResultContracts.TakePicture(), saved -> {
if (saved) {
imageView.setImageURI(null); // forget the previous photo at the same address
imageView.setImageURI(photoUri);
placeholder.setVisibility(View.GONE);
btnShare.setEnabled(true);
} else {
Toast.makeText(this, R.string.cancelled, Toast.LENGTH_SHORT).show();
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets bars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(bars.left, bars.top, bars.right, bars.bottom);
return insets;
});
btnpicture = findViewById(R.id.btncamera_id);
btnShare = findViewById(R.id.btnshare_id);
imageView = findViewById(R.id.image);
placeholder = findViewById(R.id.placeholder);
photoUri = newPhotoUri();
btnpicture.setOnClickListener(v -> {
try {
takePicture.launch(photoUri);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.no_camera, Toast.LENGTH_SHORT).show();
}
});
btnShare.setOnClickListener(v -> sharePhoto());
}
/** A file in our private cache, plus a content:// address the camera app is allowed to write to. */
private Uri newPhotoUri() {
File folder = new File(getCacheDir(), "images");
//noinspection ResultOfMethodCallIgnored
folder.mkdirs();
File file = new File(folder, "captured_image.jpg");
return FileProvider.getUriForFile(this, getPackageName() + ".provider", file);
}
private void sharePhoto() {
Intent share = new Intent(Intent.ACTION_SEND)
.setType("image/jpeg")
.putExtra(Intent.EXTRA_STREAM, photoUri)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(share, getString(R.string.share_with)));
}
}


