Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1379786
  • 博文数量: 860
  • 博客积分: 425
  • 博客等级: 下士
  • 技术积分: 1464
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-20 19:57
个人简介

对技术执着

文章分类

全部博文(860)

文章存档

2019年(16)

2018年(12)

2015年(732)

2013年(85)

2012年(15)

我的朋友

分类: LINUX

2015-07-08 17:39:55

--- 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();
阅读(795) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~