From 54f86c5f2ea71eec97f280d17780e522b9842a04 Mon Sep 17 00:00:00 2001 From: florent champigny Date: Tue, 14 Feb 2017 10:20:44 +0100 Subject: [PATCH] Storage location #7 --- .../camerafragment/sample/MainActivity.java | 4 +- .../camerafragment/CameraFragmentApi.java | 4 ++ .../internal/controller/CameraController.java | 5 ++ .../controller/impl/Camera1Controller.java | 15 ++++-- .../controller/impl/Camera2Controller.java | 14 ++++- .../internal/ui/BaseAnncaFragment.java | 17 ++++--- .../internal/utils/CameraHelper.java | 51 +++++++++++++------ 7 files changed, 82 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/com/github/florent37/camerafragment/sample/MainActivity.java b/app/src/main/java/com/github/florent37/camerafragment/sample/MainActivity.java index 07bb0c4..63af546 100644 --- a/app/src/main/java/com/github/florent37/camerafragment/sample/MainActivity.java +++ b/app/src/main/java/com/github/florent37/camerafragment/sample/MainActivity.java @@ -97,7 +97,9 @@ public class MainActivity extends AppCompatActivity { public void onPhotoTaken(byte[] bytes, String filePath) { Toast.makeText(getBaseContext(), "onPhotoTaken " + filePath, Toast.LENGTH_SHORT).show(); } - }); + }, + "/storage/self/primary", + "photo0"); } } diff --git a/camerafragment/src/main/java/com/github/florent37/camerafragment/CameraFragmentApi.java b/camerafragment/src/main/java/com/github/florent37/camerafragment/CameraFragmentApi.java index 7496e8c..c33c769 100644 --- a/camerafragment/src/main/java/com/github/florent37/camerafragment/CameraFragmentApi.java +++ b/camerafragment/src/main/java/com/github/florent37/camerafragment/CameraFragmentApi.java @@ -1,5 +1,7 @@ package com.github.florent37.camerafragment; +import android.support.annotation.Nullable; + import com.github.florent37.camerafragment.listeners.CameraFragmentControlsListener; import com.github.florent37.camerafragment.listeners.CameraFragmentResultListener; import com.github.florent37.camerafragment.listeners.CameraFragmentStateListener; @@ -13,6 +15,8 @@ public interface CameraFragmentApi { void takePhotoOrCaptureVideo(CameraFragmentResultListener resultListener); + void takePhotoOrCaptureVideo(CameraFragmentResultListener resultListener, @Nullable String directoryPath, @Nullable String fileName); + void openSettingDialog(); void switchCameraTypeFrontBack(); diff --git a/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/CameraController.java b/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/CameraController.java index 83acba8..972c6bc 100644 --- a/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/CameraController.java +++ b/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/CameraController.java @@ -1,6 +1,7 @@ package com.github.florent37.camerafragment.internal.controller; import android.os.Bundle; +import android.support.annotation.Nullable; import java.io.File; @@ -23,8 +24,12 @@ public interface CameraController { void takePhoto(CameraFragmentResultListener resultListener); + void takePhoto(CameraFragmentResultListener callback, @Nullable String direcoryPath, @Nullable String fileName); + void startVideoRecord(); + void startVideoRecord(@Nullable String direcoryPath, @Nullable String fileName); + void stopVideoRecord(CameraFragmentResultListener callback); boolean isVideoRecording(); diff --git a/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/impl/Camera1Controller.java b/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/impl/Camera1Controller.java index 8a071ec..6d0b5fb 100644 --- a/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/impl/Camera1Controller.java +++ b/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/impl/Camera1Controller.java @@ -77,13 +77,23 @@ public class Camera1Controller implements CameraController, @Override public void takePhoto(CameraFragmentResultListener callback) { - outputFile = CameraHelper.getOutputMediaFile(context, Configuration.MEDIA_ACTION_PHOTO); + takePhoto(callback, null, null); + } + + @Override + public void takePhoto(CameraFragmentResultListener callback, @Nullable String direcoryPath, @Nullable String fileName) { + outputFile = CameraHelper.getOutputMediaFile(context, Configuration.MEDIA_ACTION_PHOTO, direcoryPath, fileName); cameraManager.takePhoto(outputFile, this, callback); } @Override public void startVideoRecord() { - outputFile = CameraHelper.getOutputMediaFile(context, Configuration.MEDIA_ACTION_VIDEO); + startVideoRecord(null, null); + } + + @Override + public void startVideoRecord(@Nullable String direcoryPath, @Nullable String fileName) { + outputFile = CameraHelper.getOutputMediaFile(context, Configuration.MEDIA_ACTION_VIDEO, direcoryPath, fileName); cameraManager.startVideoRecord(outputFile, this); } @@ -142,7 +152,6 @@ public class Camera1Controller implements CameraController, return currentCameraId; } - @Override public void onCameraOpened(Integer cameraId, Size previewSize, SurfaceHolder.Callback surfaceCallback) { cameraView.updateUiForMediaAction(configurationProvider.getMediaAction()); diff --git a/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/impl/Camera2Controller.java b/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/impl/Camera2Controller.java index 80fdcdd..1f3f813 100644 --- a/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/impl/Camera2Controller.java +++ b/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/impl/Camera2Controller.java @@ -74,13 +74,23 @@ public class Camera2Controller implements CameraController, @Override public void takePhoto(CameraFragmentResultListener callback) { - outputFile = CameraHelper.getOutputMediaFile(context, Configuration.MEDIA_ACTION_PHOTO); + takePhoto(callback, null, null); + } + + @Override + public void takePhoto(CameraFragmentResultListener callback, @Nullable String direcoryPath, @Nullable String fileName) { + outputFile = CameraHelper.getOutputMediaFile(context, Configuration.MEDIA_ACTION_PHOTO, direcoryPath, fileName); camera2Manager.takePhoto(outputFile, this, callback); } @Override public void startVideoRecord() { - outputFile = CameraHelper.getOutputMediaFile(context, Configuration.MEDIA_ACTION_VIDEO); + startVideoRecord(null, null); + } + + @Override + public void startVideoRecord(@Nullable String direcoryPath, @Nullable String fileName) { + outputFile = CameraHelper.getOutputMediaFile(context, Configuration.MEDIA_ACTION_VIDEO, direcoryPath, fileName); camera2Manager.startVideoRecord(outputFile, this); } diff --git a/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/ui/BaseAnncaFragment.java b/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/ui/BaseAnncaFragment.java index 8469d4e..c12c606 100644 --- a/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/ui/BaseAnncaFragment.java +++ b/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/ui/BaseAnncaFragment.java @@ -281,9 +281,14 @@ public abstract class BaseAnncaFragment extends Fragment implements Ca @Override public void takePhotoOrCaptureVideo(final CameraFragmentResultListener resultListener) { + takePhotoOrCaptureVideo(resultListener, null, null); + } + + @Override + public void takePhotoOrCaptureVideo(final CameraFragmentResultListener resultListener, @Nullable String directoryPath, @Nullable String fileName) { switch (currentMediaActionState) { case MediaAction.ACTION_PHOTO: - takePhoto(resultListener); + takePhoto(resultListener, directoryPath, fileName); break; case MediaAction.ACTION_VIDEO: switch (currentRecordState) { @@ -291,7 +296,7 @@ public abstract class BaseAnncaFragment extends Fragment implements Ca stopRecording(resultListener); break; default: - startRecording(); + startRecording(directoryPath, fileName); break; } break; @@ -589,24 +594,24 @@ public abstract class BaseAnncaFragment extends Fragment implements Ca return checkedIndex; } - protected void takePhoto(CameraFragmentResultListener callback) { + protected void takePhoto(CameraFragmentResultListener callback, @Nullable String directoryPath, @Nullable String fileName) { if (Build.VERSION.SDK_INT > MIN_VERSION_ICECREAM) { new MediaActionSound().play(MediaActionSound.SHUTTER_CLICK); } setRecordState(Record.TAKE_PHOTO_STATE); - this.cameraController.takePhoto(callback); + this.cameraController.takePhoto(callback, directoryPath, fileName); if (cameraFragmentStateListener != null) { cameraFragmentStateListener.onRecordStatePhoto(); } } - protected void startRecording() { + protected void startRecording(@Nullable String directoryPath, @Nullable String fileName) { if (Build.VERSION.SDK_INT > MIN_VERSION_ICECREAM) { new MediaActionSound().play(MediaActionSound.START_VIDEO_RECORDING); } setRecordState(Record.RECORD_IN_PROGRESS_STATE); - this.cameraController.startVideoRecord(); + this.cameraController.startVideoRecord(directoryPath, fileName); if (cameraFragmentStateListener != null) { cameraFragmentStateListener.onRecordStateVideoInProgress(); diff --git a/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/utils/CameraHelper.java b/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/utils/CameraHelper.java index 681be86..701f025 100644 --- a/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/utils/CameraHelper.java +++ b/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/utils/CameraHelper.java @@ -8,9 +8,12 @@ import android.hardware.camera2.CameraManager; import android.media.CamcorderProfile; import android.os.Build; import android.os.Environment; +import android.support.annotation.Nullable; import android.text.TextUtils; import android.util.Log; +import com.github.florent37.camerafragment.configuration.Configuration; + import java.io.File; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -20,8 +23,6 @@ import java.util.Comparator; import java.util.Date; import java.util.List; -import com.github.florent37.camerafragment.configuration.Configuration; - /* * Created by memfis on 7/6/16. *

@@ -30,10 +31,10 @@ import com.github.florent37.camerafragment.configuration.Configuration; public final class CameraHelper { public final static String TAG = "CameraHelper"; + private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); private CameraHelper() { - } public static boolean hasCamera(Context context) { @@ -72,9 +73,14 @@ public final class CameraHelper { } } - public static File getOutputMediaFile(Context context, @Configuration.MediaAction int mediaAction) { - File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory( - Environment.DIRECTORY_PICTURES), context.getPackageName()); + public static File generateStorageDir(Context context, @Nullable String pathToDirectory) { + File mediaStorageDir = null; + if (pathToDirectory != null) { + mediaStorageDir = new File(pathToDirectory); + } else { + new File(Environment.getExternalStoragePublicDirectory( + Environment.DIRECTORY_PICTURES), context.getPackageName()); + } if (!mediaStorageDir.exists()) { if (!mediaStorageDir.mkdirs()) { @@ -83,16 +89,29 @@ public final class CameraHelper { } } - String timeStamp = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(new Date()); - File mediaFile; - if (mediaAction == Configuration.MEDIA_ACTION_PHOTO) { - mediaFile = new File(mediaStorageDir.getPath() + File.separator + - "IMG_" + timeStamp + ".jpg"); - } else if (mediaAction == Configuration.MEDIA_ACTION_VIDEO) { - mediaFile = new File(mediaStorageDir.getPath() + File.separator + - "VID_" + timeStamp + ".mp4"); - } else { - return null; + return mediaStorageDir; + } + + public static File getOutputMediaFile(Context context, @Configuration.MediaAction int mediaAction, @Nullable String pathToDirectory, @Nullable String fileName) { + final File mediaStorageDir = generateStorageDir(context, pathToDirectory); + File mediaFile = null; + + if (mediaStorageDir != null) { + if (fileName == null) { + final String timeStamp = simpleDateFormat.format(new Date()); + if (mediaAction == Configuration.MEDIA_ACTION_PHOTO) { + fileName = "IMG_" + timeStamp; + } else if (mediaAction == Configuration.MEDIA_ACTION_VIDEO) { + fileName = "VID_" + timeStamp; + } + + } + final String mediaStorageDirPath = mediaStorageDir.getPath(); + if (mediaAction == Configuration.MEDIA_ACTION_PHOTO) { + mediaFile = new File(mediaStorageDirPath + File.separator + fileName + ".jpg"); + } else if (mediaAction == Configuration.MEDIA_ACTION_VIDEO) { + mediaFile = new File(mediaStorageDirPath + File.separator + fileName + ".mp4"); + } } return mediaFile;