android 拍照上传照片(新)

来源:互联网 发布:万游网络传奇 编辑:程序博客网 时间:2024/05/01 11:53

http://blog.csdn.net/yaoyeyzq/article/details/7833772

前段时间写过一片关于照片上传的文章,但是后来发现用那种方式上传的图片是经过android系统处理过的,并不是原图,也就是说经过压缩过的,图片会变得很小,今天我就是为了解决这个问题用另外一种方式实现。

        首先当我们要得到原有的照片必须为拍照后的照片指定存放的路径地址,这个地址是在Intent中指定,方法是intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));

其中file就是手机本地的对应的输出文件,这个需要在传入参数前就生成,不然会报FileNotFoundException,下面是源码:

[java] view plaincopy
  1.                             destoryBimap();  
  2. String state = Environment.getExternalStorageState();  
  3. if (state.equals(Environment.MEDIA_MOUNTED)) {  
  4.     String saveDir = Environment.getExternalStorageDirectory()  
  5.             + "/temple";  
  6.     File dir = new File(saveDir);  
  7.     if (!dir.exists()) {  
  8.         dir.mkdir();  
  9.     }  
  10.     file = new File(saveDir, "temp.jpg");  
  11.     file.delete();  
  12.     if (!file.exists()) {  
  13.         try {  
  14.             file.createNewFile();  
  15.         } catch (IOException e) {  
  16.             e.printStackTrace();  
  17.             Toast.makeText(ReportSurveyPointActivity.this,  
  18.                     R.string.photo_file_isnull,  
  19.                     Toast.LENGTH_LONG).show();  
  20.             return;  
  21.         }  
  22.     }  
  23.     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  24.     intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));  
  25.     startActivityForResult(intent, REQUEST_CODE);  
  26. else {  
  27.     Toast.makeText(ReportSurveyPointActivity.this,  
  28.             R.string.common_msg_nosdcard, Toast.LENGTH_LONG)  
  29.             .show();  
  30. }  

其中destoryBimap()这个方法已经在上一篇文章中介绍过了,是为了把照片从手机内存中清楚调,不然黑容易报内存溢出异常,毕竟原图片比较大。下面就是判断手机是否装载SDCard,接下来在SDCard上生成我们照片保存的地址。

        那么接下来就是在onActivityResult()方法中加载我们的照片,这下加载照片就非常简单了,因为在拍照前我们已经给除了照片的路径,我们根据这个路径把照片加载进来就行了,下面是源码:

[java] view plaincopy
  1. protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  2.         super.onActivityResult(requestCode, resultCode, data);  
  3.         if (requestCode == REQUEST_CODE) {  
  4.             if (resultCode != RESULT_OK) {  
  5.                 return;  
  6.             }  
  7.             if (file != null && file.exists()) {  
  8.                 BitmapFactory.Options options = new BitmapFactory.Options();  
  9.                 options.inSampleSize = 2;  
  10.                 photo = BitmapFactory.decodeFile(file.getPath(), options);  
  11.                 photo_image.setBackgroundDrawable(new BitmapDrawable(photo));  
  12.                 pictureDir = file.getPath();  
  13.             } else {  
  14.                 Toast.makeText(this, R.string.photo_file_isnull,  
  15.                         Toast.LENGTH_LONG).show();  
  16.             }  
  17.         }  
  18.     }  

         这里需要说明一下的就是,当我们要在程序预览照片的时候由于照片比较大,考虑到内存问题,所以我们要把照片进行压缩再放到控件里面,压缩照片的方法就是用BitmapFactory.Options类的inSampleSize这个属性,这个属性的意思说在原照片的大小的多少比例来展示相片,也就是个百分比。我这里是设置的2,也就是说原照片的一般的大小展示。

下面是销毁照片的方法:

[java] view plaincopy
  1. /** 
  2.      * 销毁图片文件 
  3.      */  
  4.     private void destoryBimap() {  
  5.         if (photo != null && !photo.isRecycled()) {  
  6.             photo.recycle();  
  7.             photo = null;  
  8.         }  
  9.     }  

这个方法我是在每次拍照前和onDestroy()方法中都调用了的,都是处于内存考虑,如果你是dialog的话还建议在dimisslistener中调。

至于大家经常问道的上传照片到后台的方法我也顺便说下,我是用的HttpClient实现的很简单,而后台就是随便什么框架都行,下面是上传照片的方法实现:

[java] view plaincopy
  1. /** 
  2.      * 提交参数里有文件的数据 
  3.      *  
  4.      * @param url 
  5.      *            服务器地址 
  6.      * @param param 
  7.      *            参数 
  8.      * @return 服务器返回结果 
  9.      * @throws Exception 
  10.      */  
  11.     public static String uploadSubmit(String url, Map<String, String> param,  
  12.             File file) throws Exception {  
  13.         HttpPost post = new HttpPost(url);  
  14.   
  15.         MultipartEntity entity = new MultipartEntity();  
  16.         if (param != null && !param.isEmpty()) {  
  17.             for (Map.Entry<String, String> entry : param.entrySet()) {  
  18.                 if (entry.getValue() != null  
  19.                         && entry.getValue().trim().length() > 0) {  
  20.                     entity.addPart(entry.getKey(),  
  21.                             new StringBody(entry.getValue()));  
  22.                 }  
  23.             }  
  24.         }  
  25.         // 添加文件参数  
  26.         if (file != null && file.exists()) {  
  27.             entity.addPart("file"new FileBody(file));  
  28.         }  
  29.         post.setEntity(entity);  
  30.         HttpResponse response = httpClient.execute(post);  
  31.         int stateCode = response.getStatusLine().getStatusCode();  
  32.         StringBuffer sb = new StringBuffer();  
  33.         if (stateCode == HttpStatus.SC_OK) {  
  34.             HttpEntity result = response.getEntity();  
  35.             if (result != null) {  
  36.                 InputStream is = result.getContent();  
  37.                 BufferedReader br = new BufferedReader(  
  38.                         new InputStreamReader(is));  
  39.                 String tempLine;  
  40.                 while ((tempLine = br.readLine()) != null) {  
  41.                     sb.append(tempLine);  
  42.                 }  
  43.             }  
  44.         }  
  45.         post.abort();  
  46.         return sb.toString();  
  47.     }  

 源码下载:http://download.csdn.net/detail/yaoyeyzq/4878649

原创粉丝点击