From e0ca7e9effac8d1bc694c58a183f520c1c53362b Mon Sep 17 00:00:00 2001 From: Olga Miller Date: Thu, 25 Jun 2020 22:45:35 +0200 Subject: [PATCH] WaveFileOutputContext: Moved content of init-method into constuctor, create mFile for Version>=Q only if deleteFile-method is called --- .../Output/WaveFileOutputContext.java | 39 +++++++------------ 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/om/sstvencoder/Output/WaveFileOutputContext.java b/app/src/main/java/om/sstvencoder/Output/WaveFileOutputContext.java index 2e52d93..3072116 100644 --- a/app/src/main/java/om/sstvencoder/Output/WaveFileOutputContext.java +++ b/app/src/main/java/om/sstvencoder/Output/WaveFileOutputContext.java @@ -36,6 +36,11 @@ public class WaveFileOutputContext { public WaveFileOutputContext(ContentResolver contentResolver, String fileName) { mContentResolver = contentResolver; mFileName = fileName; + mValues = getContentValues(fileName); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) + mUri = mContentResolver.insert(MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY), mValues); + else + mFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC), mFileName); } public String getFileName() { @@ -43,33 +48,16 @@ public class WaveFileOutputContext { } public OutputStream createWaveOutputStream() { - if (init()) { - try { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) - return mContentResolver.openOutputStream(mUri); - else - return new FileOutputStream(mFile); - } catch (Exception ignore) { - } + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) + return mContentResolver.openOutputStream(mUri); + else + return new FileOutputStream(mFile); + } catch (Exception ignore) { } return null; } - private boolean init() { - mValues = getContentValues(mFileName); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - mUri = mContentResolver.insert(MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY), mValues); - if (mUri != null) { - String path = mUri.getPath(); - if (path != null) - mFile = new File(path); - } - } else { - mFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC), mFileName); - } - return mUri != null; - } - private ContentValues getContentValues(String fileName) { ContentValues values = new ContentValues(); values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/wav"); @@ -102,8 +90,9 @@ public class WaveFileOutputContext { public void deleteFile() { try { - if (mFile != null) - mFile.delete(); + if (mFile == null) + mFile = new File(mUri.getPath()); + mFile.delete(); } catch (Exception ignore) { } }