拍摄多张照片打包上传

来源:互联网 发布:淘宝需要绑定银行卡吗 编辑:程序博客网 时间:2024/04/27 14:36

package com.metarnet.activity;

 

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.metarnet.R;
import com.metarnet.customize.view.PhotoDialog;
import com.metarnet.net.HttpClientUtils;

 

public class TestActivity extends Activity  {
 private Bitmap photo;
 private ByteArrayOutputStream baos;
 private FileOutputStream fos;
 private BufferedOutputStream bos;
 private String pictureDir;
 private Button bt_photo;
 private Button bt_view;
 private Button bt_upload;
 private Intent intent;
 private int count=0;
  String saveDir = Environment.getExternalStorageDirectory()+"/TMP/";
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.test);
  bt_photo=(Button)findViewById(R.id.bt_photo);
  bt_view=(Button)findViewById(R.id.bt_view);
  bt_upload = (Button)findViewById(R.id.bt_upload);
  
  bt_photo.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    
    destoryBimap();  //拍照之前删除之前拍的,节省内存
    count++;

     String state = Environment.getExternalStorageState(); 
     if (state.equals(Environment.MEDIA_MOUNTED)) { 
        intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
         startActivityForResult(intent, 0); 

     } else { 

         Toast.makeText(TestActivity.this, 
                 "没有sd卡", Toast.LENGTH_LONG).show(); 
     }
     onActivityResult(0,1,intent);
     
   }
  });
  
  bt_view.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    
    photo = BitmapFactory.decodeFile(saveDir+"file_"+bt_photo.getId()+"_"+3+".jpg");
    Log.i("tag", "photo is :"+photo.getHeight());
    PhotoDialog dialog = new PhotoDialog(TestActivity.this, photo);
    dialog.show();
   }
  });
  
  bt_upload.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    new Thread(){
     public void run() {
      try {
       zipFile();
      } catch (Exception e) {
       e.printStackTrace();
      }
     };
    }.start();
    
    
   }
  });
  
  
 }
 
  protected void onActivityResult(int requestCode, int resultCode, Intent data)  {
         Uri uri = data.getData(); 
         if (uri != null) { 
          photo = BitmapFactory.decodeFile(uri.getPath());
         } 
         if (photo == null) { 
             Bundle bundle = data.getExtras(); 
             if (bundle != null) { 
                 photo = (Bitmap) bundle.get("data"); 
             } else { 
                 Toast.makeText(TestActivity.this, 
                         "获取照片失败", 
                         Toast.LENGTH_LONG).show(); 
                 return; 
             } 
         } 
         Log.i("tag", "photo is :"+photo.getHeight());
        
         OutPutPhoto();
     }
    
     public void OutPutPhoto(){
       try { 
        Log.i("tag", "in outPutPhoto 1----"+photo.getHeight());
           baos = new ByteArrayOutputStream(); 
           photo.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
           byte[] byteArray = baos.toByteArray(); 
//           String saveDir = Environment.getExternalStorageDirectory() 
//                   + "/temple"; 
         
          
           File dir = new File(saveDir); 
           if (!dir.exists()) { 
               dir.mkdir(); 
           } 
           File file = new File(saveDir, "file_"+bt_photo.getId()+"_"+count+".jpg"); 
           file.delete(); 
           if (!file.exists()) { 
               file.createNewFile(); 
           } 
           fos = new FileOutputStream(file); 
           bos = new BufferedOutputStream(fos); 
           bos.write(byteArray); 
           pictureDir = file.getPath(); 
           Log.i("tag", "file url is :"+file.getPath());

          
       } catch (Exception e) { 
           e.printStackTrace(); 
       } finally { 
           if (baos != null) { 
               try { 
                   baos.close(); 
               } catch (Exception e) { 
                   e.printStackTrace(); 
               } 
           } 
           if (bos != null) { 
               try { 
                   bos.close(); 
               } catch (Exception e) { 
                   e.printStackTrace(); 
               } 
           } 
           if (fos != null) { 
               try { 
                   fos.close(); 
               } catch (Exception e) { 
                   e.printStackTrace(); 
               } 
           } 
       } 
     }
    
     public void zipFile() throws Exception{
      // 定义要压缩的文件         
      File zipFile = new File(saveDir+ "file_"+bt_photo.getId()+".zip"); 
      ZipOutputStream zipOut = null;    
      // 定义压缩输出流        
      // 实例化压缩输出流对象,并指定压缩文件的输出路径         
      zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
      File file = null;
      try {
      do {
       count++;
       file = new File(saveDir, "file_"+bt_photo.getId()+"_"+count+".jpg");
       // 定义压缩文件名称         
       InputStream input = new FileInputStream(file);   
       // 定义输入文件流         
       
       // 每一个被压缩的文件都用ZipEntry表示,需要为每一个压缩后的文件设置名称        
       zipOut.putNextEntry(new ZipEntry(file.getName()));  
           int temp = 0;                            
           // 接收输入的数据         
           while ((temp = input.read()) != -1) {  
            // 读取内容            
            zipOut.write(temp);              
            // 压缩输出内容         
            }         
            input.close();                          
            // 关闭输入流        
           
   } while (file!=null&file.exists());
      } catch (FileNotFoundException e) {
    e.printStackTrace();
   }
      zipOut.close();                       
       // 关闭压缩输出流 
      Log.i("tag", "zipFile.length() is :"+zipFile.length());
      
      loadFile(zipFile);
     }
    
     public void loadFile(File tmpfile){
         Map<String, String> param = new HashMap<String, String>();
         param.put("action", "uploadFile");
         param.put("templateId", "3156");
         param.put("exeMonthId", "1036803");
         String result;
  try {
   result = HttpClientUtils.uploadSubmit("http://loaclhsot/myProject/testPhoto",
      param, tmpfile);
   Log.i("tag", "file upload result is :"+ result);
  } catch (Exception e) {
   e.printStackTrace();
  }
        
     }
   
  
    /**
      * 销毁图片文件
      */ 
     private void destoryBimap() { 
         if (photo != null && !photo.isRecycled()) { 
          Log.i("tag", "in destory 1 ------------");
             photo.recycle(); 
             photo = null; 
         } 
     }
    
    
}

原创粉丝点击