Android客制化------开机拷贝文件到内置存储(2)

之前介绍了一种通过开机启动service这样的方式来内置资源,总体看起来比较麻烦。
现在再介绍一种更改系统源码的方式来内置。因为系统在framework初始化时会启动这个MediaScannerService 故而就会进行拷贝动作
直接粗暴上源码啦

//这个类是android系统内置的fileutil类 外部是无法调用的
diff --git a/core/java/android/os/FileUtils.java b/core/java/android/os/FileUtils.java
index 0a724a1..b30735a 100644
--- a/core/java/android/os/FileUtils.java
+++ b/core/java/android/os/FileUtils.java
@@ -204,6 +204,29 @@ public class FileUtils {
         }
     }

+    /** @hide */
+    public static boolean copyFolder(File srcFile, File destFile) {
+        if (!srcFile.isDirectory()) {
+            return false;
+        }
+        if (!destFile.exists() && !destFile.mkdirs()) {
+            return false;
+        }
+        boolean result = true;
+        File[] list = srcFile.listFiles();
+        if (list == null) {
+            return result;
+        }
+        for (File f:list) {
+            if (f.isDirectory()) {
+                result &= copyFolder (f,new File(destFile,f.getName()));
+            } else {
+                result &= copyFile(f,new File(destFile,f.getName()));
+            }
+        }
+        return result;
+    }
+
     /**
      * Check if a filename is "safe" (no metacharacters or spaces).
      * @param file  The file to check
================================================================================================

diff --git a/src/com/android/providers/media/MediaScannerService.java b/src/com/android/providers/media/MediaScannerService.java
index 7391fac..dd723df 100644
--- a/src/com/android/providers/media/MediaScannerService.java
+++ b/src/com/android/providers/media/MediaScannerService.java
@@ -50,6 +50,8 @@ import java.util.concurrent.atomic.AtomicInteger;
public class MediaScannerService extends Service implements Runnable
{
     private static final String TAG = "MediaScannerService";
     //源文件路径
    +    private static String SRC = "/system/extMedia";
    + // 目的路径 当然也可以通过获取调用API的方式来获取 这里我就直接定义了
    + private static String DST= "/mnt/sdcard/";
+    private static String FLAG = "extMedia";

     private int mIndex = 0;
     private int mRecentId;
@@ -322,6 +324,14 @@ public class MediaScannerService extends Service implements Runnable
                         // scan external storage volumes
                         //directories = mExternalStoragePaths;
+                        if (!getSharedPreferences(FLAG, Context.MODE_PRIVATE)
+                                .getBoolean(FLAG, false)) {
+                            if (copyMedia(SRC,DST)) {
+                                getSharedPreferences(FLAG, Context.MODE_PRIVATE).edit()
+                                        .putBoolean(FLAG, true).commit();
+                            }
+                        }
                         String path = arguments.getString("path");
                         if (path != null) {
                             directories = new String[] {arguments.getString("path")};
@@ -347,5 +357,13 @@ public class MediaScannerService extends Service implements Runnable
             mMainLooper.post(mStopSelfTask);
         }
     };
+
+    private boolean copyMedia(String src, String dst) {
+        if (!android.os.FileUtils.copyFolder(new File(src), new File(dst))) {
+            Log.e(TAG, "copyMedia failed");
+            return false;
+        }
+        return true;
+    }
}

你可能感兴趣的:(Android客制化)