Storage location #7

pull/17/head
florent champigny 8 years ago
parent 6a8db6fc69
commit 54f86c5f2e
  1. 4
      app/src/main/java/com/github/florent37/camerafragment/sample/MainActivity.java
  2. 4
      camerafragment/src/main/java/com/github/florent37/camerafragment/CameraFragmentApi.java
  3. 5
      camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/CameraController.java
  4. 15
      camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/impl/Camera1Controller.java
  5. 14
      camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/impl/Camera2Controller.java
  6. 17
      camerafragment/src/main/java/com/github/florent37/camerafragment/internal/ui/BaseAnncaFragment.java
  7. 45
      camerafragment/src/main/java/com/github/florent37/camerafragment/internal/utils/CameraHelper.java

@ -97,7 +97,9 @@ public class MainActivity extends AppCompatActivity {
public void onPhotoTaken(byte[] bytes, String filePath) { public void onPhotoTaken(byte[] bytes, String filePath) {
Toast.makeText(getBaseContext(), "onPhotoTaken " + filePath, Toast.LENGTH_SHORT).show(); Toast.makeText(getBaseContext(), "onPhotoTaken " + filePath, Toast.LENGTH_SHORT).show();
} }
}); },
"/storage/self/primary",
"photo0");
} }
} }

@ -1,5 +1,7 @@
package com.github.florent37.camerafragment; package com.github.florent37.camerafragment;
import android.support.annotation.Nullable;
import com.github.florent37.camerafragment.listeners.CameraFragmentControlsListener; import com.github.florent37.camerafragment.listeners.CameraFragmentControlsListener;
import com.github.florent37.camerafragment.listeners.CameraFragmentResultListener; import com.github.florent37.camerafragment.listeners.CameraFragmentResultListener;
import com.github.florent37.camerafragment.listeners.CameraFragmentStateListener; import com.github.florent37.camerafragment.listeners.CameraFragmentStateListener;
@ -13,6 +15,8 @@ public interface CameraFragmentApi {
void takePhotoOrCaptureVideo(CameraFragmentResultListener resultListener); void takePhotoOrCaptureVideo(CameraFragmentResultListener resultListener);
void takePhotoOrCaptureVideo(CameraFragmentResultListener resultListener, @Nullable String directoryPath, @Nullable String fileName);
void openSettingDialog(); void openSettingDialog();
void switchCameraTypeFrontBack(); void switchCameraTypeFrontBack();

@ -1,6 +1,7 @@
package com.github.florent37.camerafragment.internal.controller; package com.github.florent37.camerafragment.internal.controller;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable;
import java.io.File; import java.io.File;
@ -23,8 +24,12 @@ public interface CameraController<CameraId> {
void takePhoto(CameraFragmentResultListener resultListener); void takePhoto(CameraFragmentResultListener resultListener);
void takePhoto(CameraFragmentResultListener callback, @Nullable String direcoryPath, @Nullable String fileName);
void startVideoRecord(); void startVideoRecord();
void startVideoRecord(@Nullable String direcoryPath, @Nullable String fileName);
void stopVideoRecord(CameraFragmentResultListener callback); void stopVideoRecord(CameraFragmentResultListener callback);
boolean isVideoRecording(); boolean isVideoRecording();

@ -77,13 +77,23 @@ public class Camera1Controller implements CameraController<Integer>,
@Override @Override
public void takePhoto(CameraFragmentResultListener callback) { 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); cameraManager.takePhoto(outputFile, this, callback);
} }
@Override @Override
public void startVideoRecord() { 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); cameraManager.startVideoRecord(outputFile, this);
} }
@ -142,7 +152,6 @@ public class Camera1Controller implements CameraController<Integer>,
return currentCameraId; return currentCameraId;
} }
@Override @Override
public void onCameraOpened(Integer cameraId, Size previewSize, SurfaceHolder.Callback surfaceCallback) { public void onCameraOpened(Integer cameraId, Size previewSize, SurfaceHolder.Callback surfaceCallback) {
cameraView.updateUiForMediaAction(configurationProvider.getMediaAction()); cameraView.updateUiForMediaAction(configurationProvider.getMediaAction());

@ -74,13 +74,23 @@ public class Camera2Controller implements CameraController<String>,
@Override @Override
public void takePhoto(CameraFragmentResultListener callback) { 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); camera2Manager.takePhoto(outputFile, this, callback);
} }
@Override @Override
public void startVideoRecord() { 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); camera2Manager.startVideoRecord(outputFile, this);
} }

@ -281,9 +281,14 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment implements Ca
@Override @Override
public void takePhotoOrCaptureVideo(final CameraFragmentResultListener resultListener) { 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) { switch (currentMediaActionState) {
case MediaAction.ACTION_PHOTO: case MediaAction.ACTION_PHOTO:
takePhoto(resultListener); takePhoto(resultListener, directoryPath, fileName);
break; break;
case MediaAction.ACTION_VIDEO: case MediaAction.ACTION_VIDEO:
switch (currentRecordState) { switch (currentRecordState) {
@ -291,7 +296,7 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment implements Ca
stopRecording(resultListener); stopRecording(resultListener);
break; break;
default: default:
startRecording(); startRecording(directoryPath, fileName);
break; break;
} }
break; break;
@ -589,24 +594,24 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment implements Ca
return checkedIndex; 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) { if (Build.VERSION.SDK_INT > MIN_VERSION_ICECREAM) {
new MediaActionSound().play(MediaActionSound.SHUTTER_CLICK); new MediaActionSound().play(MediaActionSound.SHUTTER_CLICK);
} }
setRecordState(Record.TAKE_PHOTO_STATE); setRecordState(Record.TAKE_PHOTO_STATE);
this.cameraController.takePhoto(callback); this.cameraController.takePhoto(callback, directoryPath, fileName);
if (cameraFragmentStateListener != null) { if (cameraFragmentStateListener != null) {
cameraFragmentStateListener.onRecordStatePhoto(); cameraFragmentStateListener.onRecordStatePhoto();
} }
} }
protected void startRecording() { protected void startRecording(@Nullable String directoryPath, @Nullable String fileName) {
if (Build.VERSION.SDK_INT > MIN_VERSION_ICECREAM) { if (Build.VERSION.SDK_INT > MIN_VERSION_ICECREAM) {
new MediaActionSound().play(MediaActionSound.START_VIDEO_RECORDING); new MediaActionSound().play(MediaActionSound.START_VIDEO_RECORDING);
} }
setRecordState(Record.RECORD_IN_PROGRESS_STATE); setRecordState(Record.RECORD_IN_PROGRESS_STATE);
this.cameraController.startVideoRecord(); this.cameraController.startVideoRecord(directoryPath, fileName);
if (cameraFragmentStateListener != null) { if (cameraFragmentStateListener != null) {
cameraFragmentStateListener.onRecordStateVideoInProgress(); cameraFragmentStateListener.onRecordStateVideoInProgress();

@ -8,9 +8,12 @@ import android.hardware.camera2.CameraManager;
import android.media.CamcorderProfile; import android.media.CamcorderProfile;
import android.os.Build; import android.os.Build;
import android.os.Environment; import android.os.Environment;
import android.support.annotation.Nullable;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.github.florent37.camerafragment.configuration.Configuration;
import java.io.File; import java.io.File;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
@ -20,8 +23,6 @@ import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import com.github.florent37.camerafragment.configuration.Configuration;
/* /*
* Created by memfis on 7/6/16. * Created by memfis on 7/6/16.
* <p/> * <p/>
@ -30,10 +31,10 @@ import com.github.florent37.camerafragment.configuration.Configuration;
public final class CameraHelper { public final class CameraHelper {
public final static String TAG = "CameraHelper"; public final static String TAG = "CameraHelper";
private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
private CameraHelper() { private CameraHelper() {
} }
public static boolean hasCamera(Context context) { public static boolean hasCamera(Context context) {
@ -72,9 +73,14 @@ public final class CameraHelper {
} }
} }
public static File getOutputMediaFile(Context context, @Configuration.MediaAction int mediaAction) { public static File generateStorageDir(Context context, @Nullable String pathToDirectory) {
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory( File mediaStorageDir = null;
if (pathToDirectory != null) {
mediaStorageDir = new File(pathToDirectory);
} else {
new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), context.getPackageName()); Environment.DIRECTORY_PICTURES), context.getPackageName());
}
if (!mediaStorageDir.exists()) { if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) { if (!mediaStorageDir.mkdirs()) {
@ -83,16 +89,29 @@ public final class CameraHelper {
} }
} }
String timeStamp = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(new Date()); return mediaStorageDir;
File mediaFile; }
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) { if (mediaAction == Configuration.MEDIA_ACTION_PHOTO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator + mediaFile = new File(mediaStorageDirPath + File.separator + fileName + ".jpg");
"IMG_" + timeStamp + ".jpg");
} else if (mediaAction == Configuration.MEDIA_ACTION_VIDEO) { } else if (mediaAction == Configuration.MEDIA_ACTION_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator + mediaFile = new File(mediaStorageDirPath + File.separator + fileName + ".mp4");
"VID_" + timeStamp + ".mp4"); }
} else {
return null;
} }
return mediaFile; return mediaFile;

Loading…
Cancel
Save