首页 > 代码库 > Android 照相及上传

Android 照相及上传

转自:http://helloandroid.iteye.com/blog/1183437



 

 

Java代码  收藏代码
    1. package com.android.cist.camera.view;  
    2.   
    3. import java.io.ByteArrayOutputStream;  
    4. import java.io.InputStream;  
    5. import java.util.HashMap;  
    6. import java.util.Map;  
    7.   
    8. import android.app.Activity;  
    9. import android.app.AlertDialog;  
    10. import android.content.ContentResolver;  
    11. import android.content.DialogInterface;  
    12. import android.content.Intent;  
    13. import android.graphics.Bitmap;  
    14. import android.graphics.BitmapFactory;  
    15. import android.net.Uri;  
    16. import android.os.Bundle;  
    17. import android.os.Handler;  
    18. import android.os.Message;  
    19. import android.view.View;  
    20. import android.view.View.OnClickListener;  
    21. import android.widget.Button;  
    22. import android.widget.EditText;  
    23. import android.widget.ImageView;  
    24. import android.widget.LinearLayout;  
    25. import android.widget.Toast;  
    26.   
    27. import com.android.cist.R;  
    28. import com.android.cist.network.HttpUtil;  
    29. import com.android.cist.network.form.FormFile;  
    30. import com.android.cist.network.form.HttpFormUtil;  
    31. import com.android.cist.util.ApplicationUtil;  
    32. import com.android.cist.util.ImageUtil;  
    33.   
    34. public class CameraActivity extends Activity implements OnClickListener{  
    35.     private ImageView preview;  
    36.     private EditText carNo;  
    37.     private EditText addres;  
    38.     private EditText type;  
    39.     private Button back;  
    40.     private Button submit;  
    41.     private Button capture;  
    42.     private Button reCapture;  
    43.     private OnClickListener imgViewListener;  
    44.     private Bitmap myBitmap;  
    45.     private byte[] mContent;  
    46.     private Handler handler;  
    47.     String returnString;  
    48.     private CalendarView happenDate;  
    49.     private static final int REQUEST_CAMERA = 1;  
    50.     private static final int REQUEST_CALENDAR = 2;  
    51.   
    52.     @ Override  
    53.     public void onCreate ( Bundle savedInstanceState )  
    54.     {  
    55.         super.onCreate(savedInstanceState);  
    56.         setContentView(R.layout.camera);  
    57.         setUpViews();  
    58.         setUpListeners();  
    59.         ApplicationUtil.getInstance().addActivity(this);  
    60.     }  
    61.   
    62.     @ Override  
    63.     protected void onActivityResult ( int requestCode , int resultCode , Intent data )  
    64.     {  
    65.         super.onActivityResult(requestCode, resultCode, data);  
    66.   
    67.         ContentResolver resolver = getContentResolver();  
    68.         /** 
    69.          * 因为两种方式都用到了startActivityForResult方法, 
    70.          * 这个方法执行完后都会执行onActivityResult方法, 所以为了区别到底选择了那个方式获取图片要进行判断, 
    71.          * 这里的requestCode跟startActivityForResult里面第二个参数对应 
    72.          */  
    73.         if (requestCode == 0)  
    74.         {  
    75.             try  
    76.             {  
    77.                 // 获得图片的uri  
    78.                 Uri originalUri = data.getData();  
    79.                 // 将图片内容解析成字节数组  
    80.                 mContent = readStream(resolver.openInputStream(Uri.parse(originalUri.toString())));  
    81.                 // 将字节数组转换为ImageView可调用的Bitmap对象  
    82.                 myBitmap = getPicFromBytes(mContent, null);  
    83.                 // //把得到的图片绑定在控件上显示  
    84.                 preview.setImageBitmap(myBitmap);  
    85.             } catch ( Exception e )  
    86.             {  
    87.                 System.out.println(e.getMessage());  
    88.             }  
    89.   
    90.         } else if (requestCode == REQUEST_CAMERA){  
    91.             try  
    92.             {  
    93.                 super.onActivityResult(requestCode, resultCode, data);  
    94.                 Bundle extras = data.getExtras();  
    95.                 myBitmap = (Bitmap) extras.get("data");  
    96.                 ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    97.                 myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
    98.                 mContent = baos.toByteArray();  
    99.             } catch ( Exception e )  
    100.             {  
    101.                 e.printStackTrace();  
    102.             }  
    103.             // 把得到的图片绑定在控件上显示  
    104.             preview.setImageBitmap(ImageUtil.toRoundCorner(myBitmap, 10));//把拍摄的照片转成圆角显示在预览控件上  
    105.         }else if(requestCode==REQUEST_CALENDAR){  
    106.             if(resultCode == RESULT_OK){  
    107.                 happenDate.setCalendar(data.getIntExtra("year", 1900), data.getIntExtra("month", 0), data.getIntExtra("day", 1));  
    108.             }  
    109.         }  
    110.     }  
    111.   
    112.     public static Bitmap getPicFromBytes ( byte[] bytes , BitmapFactory.Options opts )  
    113.     {  
    114.         if (bytes != null)  
    115.             if (opts != null)  
    116.                 return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts);  
    117.             else  
    118.                 return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);  
    119.         return null;  
    120.     }  
    121.   
    122.     public static byte[] readStream ( InputStream inStream ) throws Exception  
    123.     {  
    124.         byte[] buffer = new byte[1024];  
    125.         int len = -1;  
    126.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
    127.         while ((len = inStream.read(buffer)) != -1)  
    128.         {  
    129.             outStream.write(buffer, 0, len);  
    130.         }  
    131.         byte[] data = outStream.toByteArray();  
    132.         outStream.close();  
    133.         inStream.close();  
    134.         return data;  
    135.     }  
    136.   
    137.     @Override  
    138.     public void onClick(View v) {  
    139.         int id = v.getId();  
    140.         switch(id){  
    141.             case R.id.capture:{  
    142.                 final CharSequence[] items =  
    143.                 { "相册", "拍照" };  
    144.                 AlertDialog dlg = new AlertDialog.Builder(CameraActivity.this).setTitle("选择图片").setItems(items,  
    145.                         new DialogInterface.OnClickListener()  
    146.                         {  
    147.                             public void onClick ( DialogInterface dialog , int item )  
    148.                             {  
    149.                                 // 这里item是根据选择的方式,  
    150.                                 // 在items数组里面定义了两种方式,拍照的下标为1所以就调用拍照方法  
    151.                                 if (item == 1){  
    152.                                     Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");  
    153.                                     startActivityForResult(getImageByCamera, REQUEST_CAMERA);  
    154.                                 } else{  
    155.                                     Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);  
    156.                                     getImage.addCategory(Intent.CATEGORY_OPENABLE);  
    157.                                     getImage.setType("image/jpeg");  
    158.                                     startActivityForResult(getImage, 0);  
    159.                                 }  
    160.                             }  
    161.                         }).create();  
    162.                 dlg.show();  
    163.             }  
    164.             break;  
    165.             /*case R.id.reCapture:{ 
    166.                 final CharSequence[] items = 
    167.                 { "相册", "拍照" }; 
    168.                 AlertDialog dlg = new AlertDialog.Builder(CameraActivity.this).setTitle("选择图片").setItems(items, 
    169.                         new DialogInterface.OnClickListener() 
    170.                         { 
    171.                             public void onClick ( DialogInterface dialog , int item ) 
    172.                             { 
    173.                                 // 这里item是根据选择的方式, 
    174.                                 // 在items数组里面定义了两种方式,拍照的下标为1所以就调用拍照方法 
    175.                                 if (item == 1) 
    176.                                 { 
    177.                                     Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE"); 
    178.                                     startActivityForResult(getImageByCamera, 1); 
    179.                                 } else 
    180.                                 { 
    181.                                     Intent getImage = new Intent(Intent.ACTION_GET_CONTENT); 
    182.                                     getImage.addCategory(Intent.CATEGORY_OPENABLE); 
    183.                                     getImage.setType("image/jpeg"); 
    184.                                     startActivityForResult(getImage, 0); 
    185.                                 } 
    186.                             } 
    187.                         }).create(); 
    188.                 dlg.show(); 
    189.             } 
    190.             break;*/  
    191.             case R.id.submit:{  
    192.                 new PostThread().start();//开启线程提交数据  
    193.             }  
    194.             break;  
    195.             case R.id.back:{  
    196.                 CameraActivity.this.finish();//返回,销毁当前Activity  
    197.             }  
    198.             break;  
    199.             default:  
    200.             break;  
    201.         }  
    202.     }  
    203.       
    204. }  

Android 照相及上传