1. 程式人生 > >android 8.1 MTK 預設儲存(插入sdcard時預設sdcard)

android 8.1 MTK 預設儲存(插入sdcard時預設sdcard)

1.修改獲取預設儲存路徑的方法
vendor/mediatek/proprietary/frameworks/base/core/java/com/mediatek/storage/StorageManagerEx.java

method: getDefaultPath();

-        path = Environment.getExternalStorageDirectory().getAbsolutePath();
+       // path = Environment.getExternalStorageDirectory().getAbsolutePath();
+
+          path = SystemProperties.get
(PROP_SD_DEFAULT_PATH); + Log.i(TAG,"StorageManagerEx::getDefaultPath...path1:"+path); + if(path.equals("")){ + path = Environment.getExternalStorageDirectory().getAbsolutePath(); + } Log.i(TAG, " Default path taken as primary storage, path=" + path);

2.新增獲取外部儲存路徑的方法
vendor/mediatek/proprietary/frameworks/base/core/java/com/mediatek/storage/StorageManagerEx.java


 - /**
   +     * Returns external SD card path.
   +     * @hide
   +     * @internal
   +     */
   +    public static String getExternalStoragePath(Context context) {
   +                 
   +            String path = Environment.getExternalStorageDirectory().getAbsolutePath();
   +
   +            try
{ + StorageManager mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); + StorageVolume[] volumeList = mStorageManager.getVolumeList(); + final int length = volumeList.length; + for (int i = 0; i < length; i++) { + StorageVolume sv = volumeList[i]; + String getPath = sv.getPath(); + boolean removable = sv.isRemovable(); + Log.d(TAG,"StorageManagerEx::getExternalStoragePath2...getPath:"+getPath+", removable:"+removable+", length:"+length+", i:"+i); + if (removable) { + path = getPath; + } + } + + } catch (Exception e) { + e.printStackTrace(); + } + + Log.i(TAG,"path1:"+path); + if(path.equals("")){ + path = Environment.getExternalStorageDirectory().getAbsolutePath(); + } + Log.d(TAG, "getExternalStoragePath2 path=" + path); + return path; + + }

3.在Setting建立廣播監聽T卡拔插,設定預設儲存路徑
vendor/mediatek/proprietary/packages/apps/MtkSettings/AndroidManifest.xml

+        <receiver android:name=".UsbBroadCastReceiver">  
+            <intent-filter android:priority="1000">  
+                <action android:name="android.intent.action.BOOT_COMPLETED"/>  
+                <action android:name="android.intent.action.MEDIA_MOUNTED"/>  
+                <action android:name="android.intent.action.MEDIA_EJECT" />  
+                <data android:scheme="file"/>  
+            </intent-filter>  
+        </receiver>

vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/UsbBroadCastReceiver.java

**+package com.android.settings;   
+   
+import android.content.BroadcastReceiver;   
+import android.content.Context;   
+import android.content.Intent;   
+import android.provider.Settings;   
+import android.util.Log;    
+import com.mediatek.storage.StorageManagerEx;   
+   
+public class UsbBroadCastReceiver extends BroadcastReceiver {   
+    String TAG = "UsbBroadCastReceiver";   
+    boolean DEBUG = true; 
+    String externalStoragePath = "";   
+   
+   
+    public void onReceive(Context context, Intent intent) {   
+ 
+       String action = intent.getAction(); 
+ 
+       if(DEBUG) Log.d(TAG,"settings_UsbBroadCastReceiver::onReceive...action:"+action); 
+ 
+       if(Intent.ACTION_MEDIA_MOUNTED.equals(action)){ 
+ 
+               if(DEBUG) Log.d(TAG,"settings_UsbBroadCastReceiver======setDefaultPath..."); 
+ 
+                       externalStoragePath = StorageManagerEx.getExternalStoragePath(context); 
+                        
+               if(null!=externalStoragePath && !"".equals(externalStoragePath) ){ 
+               StorageManagerEx.setDefaultPath(externalStoragePath);  
+               }        
+                        
+                             if(DEBUG) Log.d(TAG,"settings_UsbBroadCastReceiver::onReceive...externalStoragePath:"+externalStoragePath+", defaultPath:"+StorageManagerEx.getDefaultPath());  
+       } 
+ 
+ 
+                
+      //  if (Intent.ACTION_MEDIA_MOUNTED.equals(intent.getAction())) {   
+      //      StorageManagerEx.setDefaultPath(ExternalStoragePath);   
+       // }   
+   
+          
+    }   
+   
+}**  
                                        hayppy children's day.