diff --git a/app/build.gradle b/app/build.gradle index f28f758..5d14a24 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,8 +8,8 @@ android { applicationId "com.github.axet.audiorecorder" minSdkVersion 9 targetSdkVersion 23 - versionCode 165 - versionName "3.0.0" + versionCode 166 + versionName "3.0.1" } signingConfigs { release { @@ -43,5 +43,5 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' - compile 'com.github.axet:android-audio-library:1.0.0' // compile project(':android-audio-library') + compile 'com.github.axet:android-audio-library:1.0.1' // compile project(':android-audio-library') } diff --git a/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java b/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java index c4133b2..4f6ef6b 100644 --- a/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java +++ b/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java @@ -837,7 +837,16 @@ public class RecordingActivity extends AppCompatActivity { void encoding(final Runnable run) { final File in = storage.getTempRecording(); - final File out = storage.getTempEncoding(); + final File out; + + final String s = targetUri.getScheme(); + if (s.startsWith(ContentResolver.SCHEME_CONTENT)) { + out = storage.getTempEncoding(); + } else if (s.startsWith(ContentResolver.SCHEME_FILE)) { + out = new File(targetUri.getPath()); + } else { + throw new RuntimeException("unkonwn uri"); + } File parent = out.getParentFile(); @@ -876,41 +885,42 @@ public class RecordingActivity extends AppCompatActivity { } }, new Runnable() { @Override - public void run() { + public void run() { // success d.cancel(); - ContentResolver resolver = getContentResolver(); - try { - InputStream is = new FileInputStream(out); - OutputStream os = resolver.openOutputStream(targetUri); - IOUtils.copy(is, os); - is.close(); - os.close(); - } catch (IOException e) { - storage.delete(out); // delete tmp encoding file + if (s.startsWith(ContentResolver.SCHEME_CONTENT)) { + ContentResolver resolver = getContentResolver(); try { - storage.delete(targetUri); - } catch (RuntimeException ee) { - Log.d(TAG, "unable to delete target uri", e); // ignore, not even created? + InputStream is = new FileInputStream(out); + OutputStream os = resolver.openOutputStream(targetUri); + IOUtils.copy(is, os); + is.close(); + os.close(); + storage.delete(out); // delete tmp encoding file + } catch (IOException e) { + storage.delete(out); // delete tmp encoding file + try { + storage.delete(targetUri); // delete SAF encoding file + } catch (RuntimeException ee) { + Log.d(TAG, "unable to delete target uri", e); // ignore, not even created? + } + Error(e); + d.cancel(); + return; } - Error(e); - d.cancel(); - return; } storage.delete(in); // delete raw recording - String n = out.getName(); - storage.delete(out); // delete tmp encoding file SharedPreferences.Editor edit = shared.edit(); - edit.putString(MainApplication.PREFERENCE_LAST, n); + edit.putString(MainApplication.PREFERENCE_LAST, Storage.getDocumentName(targetUri)); edit.commit(); run.run(); } }, new Runnable() { @Override - public void run() { + public void run() { // done d.cancel(); Error(encoder.getException()); } diff --git a/app/src/main/java/com/github/axet/audiorecorder/app/Storage.java b/app/src/main/java/com/github/axet/audiorecorder/app/Storage.java index 6b7f477..960b179 100644 --- a/app/src/main/java/com/github/axet/audiorecorder/app/Storage.java +++ b/app/src/main/java/com/github/axet/audiorecorder/app/Storage.java @@ -15,8 +15,6 @@ import java.util.Date; public class Storage extends com.github.axet.audiolibrary.app.Storage { - public static final String TMP_ENC = "encoding.data"; - public Storage(Context context) { super(context); } @@ -39,8 +37,9 @@ public class Storage extends com.github.axet.audiolibrary.app.Storage { if (Build.VERSION.SDK_INT >= 21 && s.startsWith(ContentResolver.SCHEME_CONTENT)) { Uri n = getNextFile(path, format, ext); String d = getDocumentName(n); + String ee = getExt(n); Uri docUri = DocumentsContract.buildDocumentUriUsingTree(path, DocumentsContract.getTreeDocumentId(path)); - String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(d); + String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ee); Uri childrenUri = DocumentsContract.createDocument(context.getContentResolver(), docUri, mime, d); return childrenUri; } else if (s.startsWith(ContentResolver.SCHEME_FILE)) { @@ -54,34 +53,4 @@ public class Storage extends com.github.axet.audiolibrary.app.Storage { } } - - public File getTempEncoding() { - File internal = new File(context.getCacheDir(), TMP_ENC); - if (internal.exists()) - return internal; - - // Starting in KITKAT, no permissions are required to read or write to the returned path; - // it's always accessible to the calling app. - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { - if (!permitted(context, PERMISSIONS)) - return internal; - } - - File c = context.getExternalCacheDir(); - if (c == null) // some old phones <15API with disabled sdcard return null - return internal; - - File external = new File(c, TMP_ENC); - - if (external.exists()) - return external; - - long freeI = getFree(internal); - long freeE = getFree(external); - - if (freeI > freeE) - return internal; - else - return external; - } }