From b9e3f73ec477d273f77a3ef2a2fd83b5d41134d3 Mon Sep 17 00:00:00 2001 From: Kieren Searle Date: Wed, 28 Apr 2021 12:06:58 -0400 Subject: [PATCH 1/2] Update camera example to show all capture types (Bug HMT-5460) Updated the camera example to show how to capture: * Photos where the camera returns a bitmap * Photos when the app passes a content provided URI to the camera to write the file to * Videos where the camera creates the file and passes it back to the calling app * Videos where the calling app creates the file and passes it to the camera to write the video to --- hmt1developerexamples/build.gradle | 4 +- .../hmt1developerexamples/CameraActivity.java | 139 ++++++++++++------ .../src/main/res/layout/camera_main.xml | 58 ++++++-- .../src/main/res/values/strings.xml | 6 +- 4 files changed, 145 insertions(+), 62 deletions(-) diff --git a/hmt1developerexamples/build.gradle b/hmt1developerexamples/build.gradle index 90c86b0..cbad591 100644 --- a/hmt1developerexamples/build.gradle +++ b/hmt1developerexamples/build.gradle @@ -1,12 +1,12 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 28 + compileSdkVersion 30 defaultConfig { applicationId "com.realwear.hmt1developerexamples" minSdkVersion 23 - targetSdkVersion 28 + targetSdkVersion 30 versionCode 130 versionName "1.3.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" diff --git a/hmt1developerexamples/src/main/java/com/realwear/hmt1developerexamples/CameraActivity.java b/hmt1developerexamples/src/main/java/com/realwear/hmt1developerexamples/CameraActivity.java index 2d90d50..c428324 100644 --- a/hmt1developerexamples/src/main/java/com/realwear/hmt1developerexamples/CameraActivity.java +++ b/hmt1developerexamples/src/main/java/com/realwear/hmt1developerexamples/CameraActivity.java @@ -1,43 +1,57 @@ -/** - * RealWear Development Software, Source Code and Object Code +/* + * RealWear Development Software, Source Code and Object Code. * (c) RealWear, Inc. All rights reserved. - *

+ * * Contact info@realwear.com for further information about the use of this code. */ package com.realwear.hmt1developerexamples; import android.app.Activity; +import android.content.ContentValues; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; -import androidx.core.content.FileProvider; + +import android.util.Log; import android.view.View; import android.view.WindowManager; import android.widget.ImageView; -import java.io.File; import java.util.UUID; import static android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION; /** - * Activity that shows how to use the camera to take a picture on a HMT-1 device + * Activity that shows how to use the camera to take a pictures and record videos on a HMT-1 device. */ public class CameraActivity extends Activity { + private static final String TAG = "CameraActivity"; + + // Request code for playing back videos. + private static final int FILE_PLAYBACK_REQUEST_CODE = 5; - // Request code identifying camera events - private static final int BASIC_CAMERA_REQUEST_CODE = 1889; - private static final int FILEPROVIDER_CAMERA_REQUEST_CODE = 1998; + // + // Request codes for identifying camera events. + // + private static final int BITMAP_PHOTO_REQUEST_CODE = 1; + private static final int FILE_PROVIDER_PHOTO_REQUEST_CODE = 2; + private static final int BASIC_VIDEO_REQUEST_CODE = 3; + private static final int FILE_PROVIDER_VIDEO_REQUEST_CODE = 4; + + // + // Locations for store content provided images and videos. + // + private static final String DEFAULT_IMAGE_LOCATION = Environment.DIRECTORY_DCIM + "/Camera"; + private static final String DEFAULT_VIDEO_LOCATION = Environment.DIRECTORY_MOVIES + "/Camera"; // Identifier for the image returned by the camera private static final String EXTRA_RESULT = "data"; private ImageView mImageView; - private Uri contentUri; /** * Called when the activity is created @@ -53,66 +67,107 @@ protected void onCreate(Bundle savedInstanceState) { WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.camera_main); - mImageView = (ImageView) findViewById(R.id.camera_image_view); + mImageView = findViewById(R.id.camera_image_view); } /** - * Listener for when the basic camera button is clicked + * Listener for when the bitmap photo button is clicked. * - * @param view The launch camera button + * @param view The button. */ - public void onLaunchCameraBasic(View view) { - Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // OR ACTION_VIDEO_CAPTURE - startActivityForResult(intent, BASIC_CAMERA_REQUEST_CODE); + public void onLaunchBitmapPhoto(View view) { + final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); + startActivityForResult(intent, BITMAP_PHOTO_REQUEST_CODE); } /** - * Listener for when the FileProvider camera button is clicked + * Listener for when the file provider photo button is clicked. * - * @param view The FileProvider camera button + * @param view The button. */ - public void onLaunchCameraFileProvider(View view) { + public void onLaunchFileProviderPhoto(View view) { + final String fileName = "devexamples-" + UUID.randomUUID() + ".jpg"; - final File mediaStorageDir = getExternalFilesDir(Environment.DIRECTORY_DCIM); + final ContentValues contentValues = new ContentValues(); + contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName); + contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg"); + contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, DEFAULT_IMAGE_LOCATION); - final File file = new File(mediaStorageDir, "devexamples-" + UUID.randomUUID() + ".jpg"); + final Uri contentUri = getBaseContext().getContentResolver().insert( + MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues); - contentUri = FileProvider.getUriForFile( - getApplicationContext(), - getApplicationContext().getPackageName() + ".fileprovider", - file); + final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); + captureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri); - final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // OR ACTION_VIDEO_CAPTURE - captureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); - captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri); + startActivityForResult(captureIntent, FILE_PROVIDER_PHOTO_REQUEST_CODE); + } - startActivityForResult(captureIntent, FILEPROVIDER_CAMERA_REQUEST_CODE); + /** + * Listener for when the bitmap photo button is clicked. + * + * @param view The button. + */ + public void onLaunchBasicVideo(View view) { + final Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); + startActivityForResult(intent, BASIC_VIDEO_REQUEST_CODE); + } + + /** + * Listener for when the file provider video button is clicked. + * + * @param view The button. + */ + public void onLaunchFileProviderVideo(View view) { + final String fileName = "devexamples-" + UUID.randomUUID() + ".mp4"; + + final ContentValues contentValues = new ContentValues(); + contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName); + contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4"); + contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, DEFAULT_VIDEO_LOCATION); + + final Uri contentUri = getBaseContext().getContentResolver().insert( + MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValues); + + final Intent captureIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); + captureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri); + + startActivityForResult(captureIntent, FILE_PROVIDER_VIDEO_REQUEST_CODE); } /** * Listener for result from external activities. Receives image data from camera. * - * @param requestCode See Android docs - * @param resultCode See Android docs - * @param data See Android docs + * @param requestCode See Android docs. + * @param resultCode See Android docs. + * @param data See Android docs. */ public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK && data != null) { - switch(requestCode) { - case BASIC_CAMERA_REQUEST_CODE: // Display Bitmap received from Camera - Bitmap photo = data.getExtras().getParcelable(EXTRA_RESULT); + switch (requestCode) { + case BITMAP_PHOTO_REQUEST_CODE: + final Bitmap photo = data.getExtras().getParcelable(EXTRA_RESULT); mImageView.setImageBitmap(photo); break; - case FILEPROVIDER_CAMERA_REQUEST_CODE: // View saved file in DocumentViewer + case FILE_PROVIDER_PHOTO_REQUEST_CODE: + final Uri photoUri = data.getData(); + mImageView.setImageURI(photoUri); + break; - Intent intent = new Intent(); - intent.setAction(Intent.ACTION_VIEW); - intent.addFlags(FLAG_GRANT_READ_URI_PERMISSION); - intent.setDataAndType(contentUri, "image/*"); - intent.putExtra("zoom","2"); + case BASIC_VIDEO_REQUEST_CODE: + case FILE_PROVIDER_VIDEO_REQUEST_CODE: + final Uri videoUri = data.getData(); - startActivityForResult(intent, 1234); + final Intent basicVideoPlayIntent = new Intent(); + basicVideoPlayIntent.setAction(Intent.ACTION_VIEW); + basicVideoPlayIntent.addFlags(FLAG_GRANT_READ_URI_PERMISSION); + basicVideoPlayIntent.setDataAndType(videoUri, "video/*"); + startActivityForResult(basicVideoPlayIntent, FILE_PLAYBACK_REQUEST_CODE); + break; + default: + Log.e(TAG, "Unknown request code: " + requestCode); break; } } diff --git a/hmt1developerexamples/src/main/res/layout/camera_main.xml b/hmt1developerexamples/src/main/res/layout/camera_main.xml index 612fdf7..5f0588d 100644 --- a/hmt1developerexamples/src/main/res/layout/camera_main.xml +++ b/hmt1developerexamples/src/main/res/layout/camera_main.xml @@ -1,12 +1,10 @@ - + + +