处理Assert目录读取超过1M文件

如果Assert目录文件大于1M,在读取的过程中,会出现读出来的文件,不可用。因为Assert对文件的大小有限制,这就需要特殊处理了

 

  首先是将大文件分割成小文件,然后下到本地,进行合并

 

  1.文件的分割

package com.cam;

import java.io.ByteArrayOutputStream;
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 android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.media.ThumbnailUtils;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class TestCameraActivity extends Activity {
    /** Called when the activity is first created. */
	private Button button1,button2;
	private static final int CAPTURE_CODE = 0;
	private static final int CAPTURE_CODE2 = 1;
	byte[] photo; 
	String[] data ;
	String path = "/sdcard/yjtdownload/";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        AssetManager assetManager = getAssets();
        try {
			data = assetManager.list("");
			System.out.println("----------"+data.length);
			 bind();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
       
}
    /**文件的分割**/
    private void spletar(){

    	
		//需要分割的文件放置的路径
		String path = "/sdcard/yjtdownload/";
		//需要分割的文件的文件名称
		String base = "office";
		//需要分割的文件的后缀名
		String ext = ".apk";
		//以每个小文件1024*1024字节即1M的标准来分割
		int split = 1024 * 1024;
		byte[] buf = new byte[1024];
		int num = 1;
		//建立输入流
		File inFile = new File(path + base + ext);
		FileInputStream fis;
		try {
			fis = new FileInputStream(inFile);
			while (true) {
				//以"demo"+num+".db"方式来命名小文件即分割后为demo1.db,demo2.db,。。。。。。
				FileOutputStream fos = new FileOutputStream(new File(path + base
				+ num + ext));
				for (int i = 0; i < split / buf.length; i++) {
				int read;
				try {
					read = fis.read(buf);
					fos.write(buf, 0, read);
					// 判断大文件读取是否结束
					if (read < buf.length) {
					fis.close();
					fos.close();
					return;
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				}
				try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				num++;
				}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		}

    /**文件的合并**/
 private void bind(){
    	if (!new File(path+"aa.apk").exists()) {
    		OutputStream out = null;
			try {
				out = new FileOutputStream(new File(path+"aa.apk") );
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		byte[] buffer = new byte[1024];
    		InputStream in;
    		int readLen = 0;
    		for(int i=0;i<data.length;i++){
    		// 获得输入流 ,注意文件的路径
    			System.out.println(i+"----合并第几个文件--------"+data[i]);
    		try {
				in = TestCameraActivity.this.getAssets().open("office"+(i+1)+".apk");
				while((readLen = in.read(buffer)) != -1){
		    		out.write(buffer, 0, readLen);
		    		}
		    		out.flush();
		    		in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		
    		}
    		// 把所有小文件都进行写操作后才关闭输出流,这样就会合并为一个文件了
    		try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		}
    }


}

  

posted on 2012-10-30 12:18  南海菜园  阅读(449)  评论(0编辑  收藏  举报

导航