fix saving videos on sd card

This commit is contained in:
tibbi 2016-11-06 11:40:56 +01:00
parent d2cbae264c
commit 0bdfb7802f
2 changed files with 37 additions and 11 deletions

View File

@ -11,6 +11,8 @@ import android.media.MediaRecorder;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Handler;
import android.os.ParcelFileDescriptor;
import android.support.v4.provider.DocumentFile;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
@ -46,7 +48,7 @@ public class Preview extends ViewGroup
private static Camera.Parameters mParameters;
private static PreviewListener mCallback;
private static MediaRecorder mRecorder;
private static String mCurVideoPath;
private static String mCurrVideoPath;
private static Point mScreenSize;
private static Uri mTargetUri;
private static Context mContext;
@ -88,7 +90,7 @@ public class Preview extends ViewGroup
mIsVideoMode = false;
mIsSurfaceCreated = false;
mSetupPreviewAfterMeasure = false;
mCurVideoPath = "";
mCurrVideoPath = "";
mScreenSize = Utils.Companion.getScreenSize(mActivity);
mContext = getContext();
initGestureDetector();
@ -661,8 +663,8 @@ public class Preview extends ViewGroup
mRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
mRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mCurVideoPath = Utils.Companion.getOutputMediaFile(mContext, false);
if (mCurVideoPath.isEmpty()) {
mCurrVideoPath = Utils.Companion.getOutputMediaFile(mContext, false);
if (mCurrVideoPath.isEmpty()) {
Utils.Companion.showToast(mContext, R.string.video_creating_error);
return false;
}
@ -672,7 +674,20 @@ public class Preview extends ViewGroup
cpHigh.videoFrameWidth = videoSize.width;
cpHigh.videoFrameHeight = videoSize.height;
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());
int rotation = getFinalRotation();
@ -682,14 +697,18 @@ public class Preview extends ViewGroup
try {
mRecorder.prepare();
} catch (Exception e) {
Utils.Companion.showToast(mContext, R.string.video_setup_error);
Log.e(TAG, "initRecorder " + e.getMessage());
releaseCamera();
setupFailed(e);
return false;
}
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() {
if (mIsRecording) {
stopRecording();
@ -724,11 +743,11 @@ public class Preview extends ViewGroup
try {
toggleShutterSound(true);
mRecorder.stop();
final String[] paths = {mCurVideoPath};
final String[] paths = {mCurrVideoPath};
MediaScannerConnection.scanFile(mContext, paths, null, this);
} catch (RuntimeException e) {
toggleShutterSound(false);
new File(mCurVideoPath).delete();
new File(mCurrVideoPath).delete();
Utils.Companion.showToast(mContext, R.string.video_saving_error);
Log.e(TAG, "stopRecording " + e.getMessage());
mRecorder = null;
@ -740,7 +759,7 @@ public class Preview extends ViewGroup
mRecorder = null;
mIsRecording = false;
final File file = new File(mCurVideoPath);
final File file = new File(mCurrVideoPath);
if (file.exists() && file.length() == 0) {
file.delete();
}

View File

@ -9,6 +9,9 @@ import android.graphics.Point
import android.hardware.Camera
import android.support.v4.content.ContextCompat
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.text.SimpleDateFormat
import java.util.*
@ -111,5 +114,9 @@ class Utils {
fun hasAudioPermission(cxt: Context): Boolean {
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)
}
}