Android Call Device Existing Camera Application Details
- 2021-12-21 05:05:48
- OfStack
If we just need to enable users to take photos, we can directly request the existing camera application to take photos and return them to us
1. Take pictures
1.1 Request camera function
In the manifest file, add:
<manifest ... >
<uses-feature android:name="android.hardware.camera"
android:required="true" />
...
</manifest>
If your application uses a camera, but can work normally without a camera, you should set
android:required
Set to
false
. In this way,
Google Play
Devices that do not have a camera will be allowed to download your application.
1.2 Call the camera application to take pictures and get thumbnails
static final int REQUEST_IMAGE_CAPTURE = 1;
// Call the camera application to take pictures
private void takePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
1.3 Call the camera application to take a picture and get a complete picture
// Call the camera application to take a picture and get a complete picture
private void takePictureGetFile() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
ex.printStackTrace();
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
this.getPackageName()+".fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQ_2);
}
}
}
1.4 Camera application returns thumbnail, complete picture
Android
The camera application will return the
Intent
(Passed to onActivityResult () as a small Bitmap in extra, using the key "data").
The following code retrieves this picture and displays it in 1 ImageView
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Get thumbnails
if (requestCode == REQ_1 && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
ivThumbnail.setImageBitmap(imageBitmap);
}
// Get the full picture
if (requestCode == REQ_2) {
FileInputStream fis = null;
try {
fis = new FileInputStream(currentPhotoPath);
Bitmap bitmap = BitmapFactory.decodeStream(fis);
ivComplete.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Watch the video
if (requestCode == REQ_3 && resultCode == RESULT_OK) {
Uri videoUri = data.getData();
vvVideo.setMediaController(new MediaController(this));
vvVideo.setVideoURI(videoUri);
vvVideo.start();
}
}
2. Save the configuration of the file
Having write permission implies that you can read, so you only need to request write permission
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>
Note:
getUriForFile
(Context, String, File) returns content://URI. For the latest applications that target Android 7.0 (API level 24) and later, passing file://URI across package boundaries results in FileUriExposedException. Therefore, a more general method to useFileProvider
Save pictures.
Add a provider to the manifest file:
<application>
...
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
...
</application>
Ensure that the authorization string is the same as the
getUriForFile
(Context, String, File). New File
res/xml/file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Android/data/com.example.package.name/files/Pictures" />
</paths>
The path component corresponds to
getExternalFilesDir()
The path returned (when Environment. DIRECTORY_PICTURES is called). Be sure to put
false
0
Replace with the actual package name of the application.
3. Record video
3.1 Call the camera application to record video
// Call the camera application to record video
private void takeVideo() {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {// If it is null, The application will crash
File videoFile = null;
try {
videoFile = createVideoFile();
} catch (IOException ex) {
ex.printStackTrace();
}
if (videoFile != null) {
Uri videoURI = FileProvider.getUriForFile(this,
this.getPackageName()+".fileprovider",
videoFile);
takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, videoURI);
startActivityForResult(takeVideoIntent, REQ_3);
}
}
}
3.2 Camera application returns video
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Watch the video
if (requestCode == REQ_3 && resultCode == RESULT_OK) {
Uri videoUri = data.getData();
vvVideo.setMediaController(new MediaController(this));
vvVideo.setVideoURI(videoUri);
vvVideo.start();
}
}