Android8.1添加MTP数据同步操作

硬件平台:展讯8541e

软件平台:Andoir8.1

需求:智能硬件连接PC之后,走MTP模式,数据不同步,重新插拔USB线也无作用,只有重启才能同步数据,因此需要添加USB事件触发同步事件。

diff --git a/src/com/android/providers/media/MtpReceiver.java b/src/com/android/providers/media/MtpReceiver.java
index ae5b490..88ded32 100755
--- a/src/com/android/providers/media/MtpReceiver.java
+++ b/src/com/android/providers/media/MtpReceiver.java
@@ -27,6 +27,8 @@ import android.os.Bundle;
 import android.os.UserHandle;
 import android.util.Log;
 import android.mtp.MtpServer;
+import android.os.Environment;
+import android.os.EnvironmentEx;

 
 public class MtpReceiver extends BroadcastReceiver {
     private static final String TAG = MtpReceiver.class.getSimpleName();
@@ -72,6 +74,12 @@ public class MtpReceiver extends BroadcastReceiver {
             }
             if (DEBUG) { Log.d(TAG, "handleUsbState startService"); }
             context.startService(intent);
+            // kbding add for mtp scanner
+            scan(context, MediaProvider.INTERNAL_VOLUME, null);
+            // scan the inner storage
+            String internalStoragePath = EnvironmentEx.getInternalStoragePath().getPath();
+            scan(context, MediaProvider.EXTERNAL_VOLUME, internalStoragePath);
+            // kbding add end

         } else if (!connected || !(mtpEnabled || ptpEnabled)) {
             // Only unbind if disconnected or disabled.
             hasConfigured = false;
@@ -92,4 +100,15 @@ public class MtpReceiver extends BroadcastReceiver {
                     "content://media/none/mtp_connected"), null);
         }
     }
+
+    private void scan(Context context, String volume, String path) {
+        Log.d(TAG, "scan " + volume + " volume path: " + (null == path
+                ? (Environment.getRootDirectory() + "/media") : path));
+        Bundle args = new Bundle();
+        args.putString("volume", volume);
+        // SPRD: Add
+        args.putString("path", path);
+        context.startService(
+                new Intent(context, MediaScannerService.class).putExtras(args));
+    }

 }

实现的思路是在USB插拔事件触发之后,会重新扫描相应的存储目录,同步数据库。

 

Mark一下~

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(android,MTP,MediaProvider,android,USB插拔,SPRD)