Android 拷贝文件夹到sdcard路径下

本文介绍了一种在Android设备上预置文件夹至SDCard路径的解决方案,通过系统级应用在设备开机完成后从指定路径拷贝并解压文件至SDCard,避免了ROM制作时无法操作的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目中遇到客户需要预置文件夹到sdcard路径下,sdcard路径是开机启动时才进行挂载,所以没有办法在ROM制作的时候进行拷贝,

我的解决方案是通过系统级应用在开机完成后从别的路径下拷贝到sdcard路径下,然后再进行解压.下面我把这个应用的代码粘贴出来,希望大家共同进步,如果有更好的解决方案,可以互相交流

应用共有三个类:

1.MainActivity.java

2.CopyIntentService.java

3.ZipUtils.java

下面就介绍这几个类,MainActivity职责就是启动CopyIntentService,代码如下:

public class MainActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		Intent i = new Intent(this, CopyIntentService.class);
		i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		i.setPackage(getPackageName());
		startService(i);
	}
}

然后是CopyIntentService类,这个类主要是处理拷贝文件和解压文件,代码如下:

public class CopyIntentService extends IntentService {

	private static String SD_INTERNAL_PATH = "/sdcard/jinghua/";
	
	private static String BACKUP_PATH = SD_INTERNAL_PATH;
	private static String FILE_NAME = "word.zip";
	public static String FLDY_PREFER = "file_copy";
	public static String IS_COPY_FULL = "is_copy_full";
	public static String IS_UNZIP_FULL = "is_unzip_full";
	private File file;
	private SharedPreferences preferences;

	public CopyIntentService() {
		super("CopyIntentService");
	}

	@Override
	public IBinder onBind(Intent intent) {
		return super.onBind(intent);
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		preferences = getSharedPreferences(CopyIntentService.FLDY_PREFER,
				Context.MODE_PRIVATE);
		if (!preferences.getBoolean(IS_COPY_FULL, false)) {
			try {
				file = new File(SD_PATH + FILE_NAME);
				if (file.exists())
					file.delete();
				file.createNewFile();
				copyAssetsZipFile();
			} catch (Exception e) {
				preferences.edit().putBoolean(IS_COPY_FULL, false).commit();
				return;
			}
		}
		if (!preferences.getBoolean(IS_UNZIP_FULL, false)) {
			try {
				ZipUtils.unZipFile(file, BACKUP_PATH);
				preferences.edit().putBoolean(IS_UNZIP_FULL, true).commit();
				file.delete();
			} catch (Exception e) {
				preferences.edit().putBoolean(IS_UNZIP_FULL, false).commit();
				return;
			}
		}
	}

	private void copyAssetsZipFile() throws IOException {
		int length = 0;
		int totalSize = 0;
		byte[] buffer = new byte[2048];
		InputStream myInput = getAssets().open(FILE_NAME);
		OutputStream myOutput = new FileOutputStream(file);
		while ((length = myInput.read(buffer)) > 0) {
			totalSize += length;
			myOutput.write(buffer, 0, length);
		}
		if (totalSize == myInput.available()
				&& myInput.available() == file.length())
			preferences.edit().putBoolean(IS_COPY_FULL, true).commit();
		else
			preferences.edit().putBoolean(IS_COPY_FULL, false).commit();
		myOutput.flush();
		myOutput.close();
		myInput.close();
	}
}

最后是ZipUtils类,这是一个工具类,主要负责将解压已拷贝到sdcard路径下的文件,代码如下:

public class ZipUtils {
	private static final int BUFF_SIZE = 1024 * 1024; // 1M Byte

	public static void zipFiles(Collection<File> resFileList, File zipFile)
			throws IOException {
		ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(
				new FileOutputStream(zipFile), BUFF_SIZE));
		zipout.setMethod(ZipOutputStream.STORED);
		for (File resFile : resFileList) {
			zipFile(resFile, zipout, "");
		}
		zipout.close();
	}

	public static void zipFiles(Collection<File> resFileList, File zipFile,
			String comment) throws IOException {
		ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(
				new FileOutputStream(zipFile), BUFF_SIZE));
		for (File resFile : resFileList) {
			zipFile(resFile, zipout, "");
		}
		zipout.setComment(comment);
		zipout.close();
	}

	public static void unZipFile(File zipFile, String folderPath)
			throws ZipException, IOException {
		File desDir = new File(folderPath);
		if (!desDir.exists()) {
			desDir.mkdirs();
		}
		ZipFile zf = new ZipFile(zipFile);
		for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
			ZipEntry entry = ((ZipEntry) entries.nextElement());
			InputStream in = zf.getInputStream(entry);
			String str = folderPath + File.separator + entry.getName();

			File desFile = new File(str);
			if (entry.isDirectory()) {
				if (!desFile.exists())
					desFile.mkdirs();
				continue;
			}
			if (!desFile.exists()) {
				File fileParentDir = desFile.getParentFile();
				if (!fileParentDir.exists()) {
					fileParentDir.mkdirs();
				}
				desFile.createNewFile();
			}
			OutputStream out = new FileOutputStream(desFile);
			byte buffer[] = new byte[BUFF_SIZE];
			int realLength;
			while ((realLength = in.read(buffer)) > 0) {
				out.write(buffer, 0, realLength);
			}
			in.close();
			out.close();
		}

	}

	public static ArrayList<File> unZipSelectedFile(File zipFile,
			String folderPath, String nameContains) throws ZipException,
			IOException {
		ArrayList<File> fileList = new ArrayList<File>();

		File desDir = new File(folderPath);
		if (!desDir.exists()) {
			desDir.mkdir();
		}

		ZipFile zf = new ZipFile(zipFile);
		for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
			ZipEntry entry = ((ZipEntry) entries.nextElement());
			if (entry.getName().contains(nameContains)) {
				InputStream in = zf.getInputStream(entry);
				String str = folderPath + File.separator + entry.getName();

				File desFile = new File(str);
				if (!desFile.exists()) {
					File fileParentDir = desFile.getParentFile();
					if (!fileParentDir.exists()) {
						fileParentDir.mkdirs();
					}
					desFile.createNewFile();
				}
				OutputStream out = new FileOutputStream(desFile);
				byte buffer[] = new byte[BUFF_SIZE];
				int realLength;
				while ((realLength = in.read(buffer)) > 0) {
					out.write(buffer, 0, realLength);
				}
				in.close();
				out.close();
				fileList.add(desFile);
			}
		}
		return fileList;
	}

	public static ArrayList<String> getEntriesNames(File zipFile)
			throws ZipException, IOException {
		ArrayList<String> entryNames = new ArrayList<String>();
		Enumeration<?> entries = getEntriesEnumeration(zipFile);
		while (entries.hasMoreElements()) {
			ZipEntry entry = ((ZipEntry) entries.nextElement());
			entryNames.add(new String(getEntryName(entry)));
		}
		return entryNames;
	}

	public static Enumeration<?> getEntriesEnumeration(File zipFile)
			throws ZipException, IOException {
		ZipFile zf = new ZipFile(zipFile);
		return zf.entries();

	}

	public static String getEntryComment(ZipEntry entry)
			throws UnsupportedEncodingException {
		return new String(entry.getComment());
	}

	public static String getEntryName(ZipEntry entry)
			throws UnsupportedEncodingException {
		return new String(entry.getName());
	}

	private static void zipFile(File resFile, ZipOutputStream zipout,
			String rootpath) throws FileNotFoundException, IOException {
		rootpath = rootpath
				+ (rootpath.trim().length() == 0 ? "" : File.separator)
				+ resFile.getName();
		rootpath = new String(rootpath);
		if (resFile.isDirectory()) {
			File[] fileList = resFile.listFiles();
			for (File file : fileList) {
				zipFile(file, zipout, rootpath);
			}
		} else {
			byte buffer[] = new byte[BUFF_SIZE];
			BufferedInputStream in = new BufferedInputStream(
					new FileInputStream(resFile), BUFF_SIZE);
			zipout.putNextEntry(new ZipEntry(rootpath));
			int realLength;
			while ((realLength = in.read(buffer)) != -1) {
				zipout.write(buffer, 0, realLength);
			}
			in.close();
			zipout.flush();
			zipout.closeEntry();
		}
	}
}

 使用方法是将你需要放到sdcard路径下的文件夹打包成zip文件,然后放到assets路径下,就可以了~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值