--- a/packages/apps/SoundRecorder/src/com/android/soundrecorder/Recorder.java
+++ b/packages/apps/SoundRecorder/src/com/android/soundrecorder/Recorder.java
@@ -9,6 +9,11 @@ import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.storage.StorageManager;
+import android.provider.MediaStore;
+import android.net.Uri;
+import android.database.Cursor;
+import android.content.ContentResolver;
+
import com.mediatek.featureoption.FeatureOption;
import java.io.File;
@@ -276,6 +281,54 @@ public class Recorder implements OnCompletionListener {
signalStateChanged(IDLE_STATE);
}
+ /**
+ * <p>
+ * Check file from Media DB, if the file is add by media scanner .
+ * @param context
+ * @param file
+ * @return return true if exited in Media DB,else return fasle.
+ */
+ private boolean checkFromMediaDB(Context context ,File file ) {
+ Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
+ final String[] ids = new String[]{MediaStore.Audio.Media._ID};
+ final String where = MediaStore.Audio.Media.DATA
+ + " LIKE '%"
+ + file.getAbsolutePath().replaceFirst(
+ "file:///", "") + "'";
+
+ Cursor cursor = query(context,base, ids, where, null, null);
+ try {
+ return null != cursor && cursor.getCount() > 0;
+ } catch (IllegalStateException e) {
+ e.printStackTrace();
+ } finally {
+ if (cursor != null) {
+ cursor.close();
+ }
+ }
+ return false;
+ }
+
+ /*
+ * A simple utility to do a query into the databases.
+ */
+ private Cursor query(Context context, Uri uri, String[] projection, String selection,
+ String[] selectionArgs, String sortOrder) {
+
+ try {
+ ContentResolver resolver = context.getContentResolver();
+ if (resolver == null) {
+ return null;
+ }
+ return resolver.query(uri, projection, selection, selectionArgs,
+ sortOrder);
+
+ } catch (UnsupportedOperationException ex) {
+ return null;
+ }
+ }
+
public void startRecording(int outputfileformat, int recordingType,
String extension, Context context) {
SRLogUtils.i(TAG, "in startRecording() 4 param");
@@ -284,9 +337,14 @@ public class Recorder implements OnCompletionListener {
&& mSampleFile.exists()
&& !(mSoundRecorderDoWhat != null && mSoundRecorderDoWhat
.equals(SoundRecorder.PLAY))) {
- if (!mSampleFile.delete()) {
- SRLogUtils.i(TAG, "<startRecording> delete file fail");
+ //if the file is existing in media db, don't delete it.
+ if(!checkFromMediaDB(context,mSampleFile)){
+ if (!mSampleFile.delete()) {
+ SRLogUtils.i(TAG, "<startRecording> delete file fail");
+ }
}
+ //End
}
stop();
解决录音文件丢失问题
最新推荐文章于 2022-03-31 22:39:42 发布