mirror of
https://github.com/SimpleMobileTools/Simple-Camera.git
synced 2025-02-02 08:16:44 +01:00
fix saving videos on sd card
This commit is contained in:
parent
d2cbae264c
commit
0bdfb7802f
@ -11,6 +11,8 @@ import android.media.MediaRecorder;
|
|||||||
import android.media.MediaScannerConnection;
|
import android.media.MediaScannerConnection;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
|
import android.os.ParcelFileDescriptor;
|
||||||
|
import android.support.v4.provider.DocumentFile;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
import android.view.ScaleGestureDetector;
|
import android.view.ScaleGestureDetector;
|
||||||
@ -46,7 +48,7 @@ public class Preview extends ViewGroup
|
|||||||
private static Camera.Parameters mParameters;
|
private static Camera.Parameters mParameters;
|
||||||
private static PreviewListener mCallback;
|
private static PreviewListener mCallback;
|
||||||
private static MediaRecorder mRecorder;
|
private static MediaRecorder mRecorder;
|
||||||
private static String mCurVideoPath;
|
private static String mCurrVideoPath;
|
||||||
private static Point mScreenSize;
|
private static Point mScreenSize;
|
||||||
private static Uri mTargetUri;
|
private static Uri mTargetUri;
|
||||||
private static Context mContext;
|
private static Context mContext;
|
||||||
@ -88,7 +90,7 @@ public class Preview extends ViewGroup
|
|||||||
mIsVideoMode = false;
|
mIsVideoMode = false;
|
||||||
mIsSurfaceCreated = false;
|
mIsSurfaceCreated = false;
|
||||||
mSetupPreviewAfterMeasure = false;
|
mSetupPreviewAfterMeasure = false;
|
||||||
mCurVideoPath = "";
|
mCurrVideoPath = "";
|
||||||
mScreenSize = Utils.Companion.getScreenSize(mActivity);
|
mScreenSize = Utils.Companion.getScreenSize(mActivity);
|
||||||
mContext = getContext();
|
mContext = getContext();
|
||||||
initGestureDetector();
|
initGestureDetector();
|
||||||
@ -661,8 +663,8 @@ public class Preview extends ViewGroup
|
|||||||
mRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
|
mRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
|
||||||
mRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
|
mRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
|
||||||
|
|
||||||
mCurVideoPath = Utils.Companion.getOutputMediaFile(mContext, false);
|
mCurrVideoPath = Utils.Companion.getOutputMediaFile(mContext, false);
|
||||||
if (mCurVideoPath.isEmpty()) {
|
if (mCurrVideoPath.isEmpty()) {
|
||||||
Utils.Companion.showToast(mContext, R.string.video_creating_error);
|
Utils.Companion.showToast(mContext, R.string.video_creating_error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -672,7 +674,20 @@ public class Preview extends ViewGroup
|
|||||||
cpHigh.videoFrameWidth = videoSize.width;
|
cpHigh.videoFrameWidth = videoSize.width;
|
||||||
cpHigh.videoFrameHeight = videoSize.height;
|
cpHigh.videoFrameHeight = videoSize.height;
|
||||||
mRecorder.setProfile(cpHigh);
|
mRecorder.setProfile(cpHigh);
|
||||||
mRecorder.setOutputFile(mCurVideoPath);
|
|
||||||
|
if (Utils.Companion.needsStupidWritePermissions(getContext(), mCurrVideoPath)) {
|
||||||
|
try {
|
||||||
|
DocumentFile document = Utils.Companion.getFileDocument(getContext(), mCurrVideoPath);
|
||||||
|
document = document.createFile("", mCurrVideoPath.substring(mCurrVideoPath.lastIndexOf('/') + 1));
|
||||||
|
final Uri uri = document.getUri();
|
||||||
|
final ParcelFileDescriptor fileDescriptor = getContext().getContentResolver().openFileDescriptor(uri, "rw");
|
||||||
|
mRecorder.setOutputFile(fileDescriptor.getFileDescriptor());
|
||||||
|
} catch (Exception e) {
|
||||||
|
setupFailed(e);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
mRecorder.setOutputFile(mCurrVideoPath);
|
||||||
|
}
|
||||||
mRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
|
mRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
|
||||||
|
|
||||||
int rotation = getFinalRotation();
|
int rotation = getFinalRotation();
|
||||||
@ -682,14 +697,18 @@ public class Preview extends ViewGroup
|
|||||||
try {
|
try {
|
||||||
mRecorder.prepare();
|
mRecorder.prepare();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Utils.Companion.showToast(mContext, R.string.video_setup_error);
|
setupFailed(e);
|
||||||
Log.e(TAG, "initRecorder " + e.getMessage());
|
|
||||||
releaseCamera();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setupFailed(Exception e) {
|
||||||
|
Utils.Companion.showToast(mContext, R.string.video_setup_error);
|
||||||
|
Log.e(TAG, "initRecorder " + e.getMessage());
|
||||||
|
releaseCamera();
|
||||||
|
}
|
||||||
|
|
||||||
public boolean toggleRecording() {
|
public boolean toggleRecording() {
|
||||||
if (mIsRecording) {
|
if (mIsRecording) {
|
||||||
stopRecording();
|
stopRecording();
|
||||||
@ -724,11 +743,11 @@ public class Preview extends ViewGroup
|
|||||||
try {
|
try {
|
||||||
toggleShutterSound(true);
|
toggleShutterSound(true);
|
||||||
mRecorder.stop();
|
mRecorder.stop();
|
||||||
final String[] paths = {mCurVideoPath};
|
final String[] paths = {mCurrVideoPath};
|
||||||
MediaScannerConnection.scanFile(mContext, paths, null, this);
|
MediaScannerConnection.scanFile(mContext, paths, null, this);
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
toggleShutterSound(false);
|
toggleShutterSound(false);
|
||||||
new File(mCurVideoPath).delete();
|
new File(mCurrVideoPath).delete();
|
||||||
Utils.Companion.showToast(mContext, R.string.video_saving_error);
|
Utils.Companion.showToast(mContext, R.string.video_saving_error);
|
||||||
Log.e(TAG, "stopRecording " + e.getMessage());
|
Log.e(TAG, "stopRecording " + e.getMessage());
|
||||||
mRecorder = null;
|
mRecorder = null;
|
||||||
@ -740,7 +759,7 @@ public class Preview extends ViewGroup
|
|||||||
mRecorder = null;
|
mRecorder = null;
|
||||||
mIsRecording = false;
|
mIsRecording = false;
|
||||||
|
|
||||||
final File file = new File(mCurVideoPath);
|
final File file = new File(mCurrVideoPath);
|
||||||
if (file.exists() && file.length() == 0) {
|
if (file.exists() && file.length() == 0) {
|
||||||
file.delete();
|
file.delete();
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,9 @@ import android.graphics.Point
|
|||||||
import android.hardware.Camera
|
import android.hardware.Camera
|
||||||
import android.support.v4.content.ContextCompat
|
import android.support.v4.content.ContextCompat
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
|
import com.simplemobiletools.camera.extensions.getFileDocument
|
||||||
|
import com.simplemobiletools.camera.extensions.isKitkat
|
||||||
|
import com.simplemobiletools.camera.extensions.isPathOnSD
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@ -111,5 +114,9 @@ class Utils {
|
|||||||
fun hasAudioPermission(cxt: Context): Boolean {
|
fun hasAudioPermission(cxt: Context): Boolean {
|
||||||
return ContextCompat.checkSelfPermission(cxt, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
|
return ContextCompat.checkSelfPermission(cxt, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun needsStupidWritePermissions(context: Context, path: String) = context.isPathOnSD(path) && context.isKitkat()
|
||||||
|
|
||||||
|
fun getFileDocument(context: Context, path: String) = context.getFileDocument(path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user