解决录音文件丢失问题

--- 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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值