Android首次开机通过设置向导拷贝文件到sdcard目录下
MTK平台机器,首次开机会启动OOBE设置向导,我们可以在此应用中增加一个界面,从/system/目录下拷贝文件到/mnt/sdcard/目录下。
1.首先编译时要将文件从代码路径拷贝到对应的out目录,可以用在mk文件中实现:
方法①:如果文件较少,可以采用逐条拷贝方式,范例如下:
CUR_PATH := vendor/ThirdParty/App/tchip PRODUCT_COPY_FILES += $(CUR_PATH)/bootanimation.zip:system/media/bootanimation.zip $(CUR_PATH)/multimedia/MP4.mp4:system/media/multimedia/MP4.mp4
方法②:如果文件结构比较复杂,可采用如下脚本:
CUR_PATH := vendor/ThirdParty/App/qifei tchip_files := $(shell ls $(CUR_PATH)/multimedia ) PRODUCT_COPY_FILES += $(foreach file, $(tchip_files), $(CUR_PATH)/multimedia/$(file):system/media/multimedia/$(file))
2.修改开机向导OOBE
mediatek/packages/apps/OOBE/
①在AndroidManifest.xml中添加权限,并且声明我们自定义的Activity:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <activity android:name=".basic.CopyFiles"></activity>
②编写Acitivity内容:
package com.mediatek.oobe.basic; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.util.Log; import android.widget.ProgressBar; import android.widget.Toast; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import com.mediatek.oobe.R; /** * Created by Alex on 2015/1/12. */ public class CopyFiles extends Activity { ProgressBar progressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.copyfiles); progressBar = (ProgressBar) findViewById(R.id.progressBar); //copy(); new Thread(new TimeThread()).start(); //copy(); } final Handler timeHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 1: // Toast.makeText(CopyFiles.this, "复制成功", Toast.LENGTH_SHORT).show(); finish(); } super.handleMessage(msg); } }; public class TimeThread implements Runnable { @Override public void run() { try { copy(); //Message message = new Message(); //message.what = 1; //timeHandler.sendMessage(message); } catch (Exception e) { e.printStackTrace(); } } } private void copy() { final String FROMPATH = "/system/media/multimedia"; //final String FROMPATH = "/mnt/sdcard/Test"; final String TOPATH_MUSIC = "/mnt/sdcard/Music"; final String TOPATH_MOVIE = "/mnt/sdcard/Movie"; String[] filename = null; File file = new File(FROMPATH); if (file.exists()) { filename = file.list(); } File filestoreMusic = new File(TOPATH_MUSIC); if (!filestoreMusic.exists()) { filestoreMusic.mkdir(); } File filestoreMovie = new File(TOPATH_MOVIE); if (!filestoreMovie.exists()) { filestoreMovie.mkdir(); } for (int i = 0; i < filename.length; i++) { Log.d("copyFiles", filename[i]); InputStream fosfrom = null; OutputStream fosto; try { fosfrom = new FileInputStream(FROMPATH + "/" + filename[i]); if(i==3) { fosto = new FileOutputStream(TOPATH_MOVIE + "/" + filename[i]); } else { fosto = new FileOutputStream(TOPATH_MUSIC + "/" + filename[i]); } byte bt[] = new byte[1024]; int c; while ((c = fosfrom.read(bt)) > 0) { fosto.write(bt, 0, c); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { fosfrom.close(); } catch (IOException e) { e.printStackTrace(); } progressBar.setProgress(i + 1); if(i== filename.length-1){ Message message = new Message(); message.what = 1; timeHandler.sendMessage(message); } } try { sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); } catch (Exception e) { e.printStackTrace(); } } }
③布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="fill_parent" android:orientation="vertical" android:gravity="center" android:layout_height="fill_parent"> <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/progressBar" android:max="6" android:layout_gravity="center_vertical" /> <TextView android:id="@+id/copyText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/file_copying" /> </LinearLayout> </LinearLayout>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。