解决通过Intent调用系统拍照程序,返回图片太小的问题[android]

转载自:http://zjf1428.iteye.com/blog/919162


以下的代码可以调用系统的拍照程序, 

Intent it = newIntent("android.media.action.IMAGE_CAPTURE"); 
startActivityForResult(it, Activity.DEFAULT_KEYS_DIALER); 
按下拍照键后,会返回到你的activity,所以你的activity要在onActivityResult方法里加一个处理, 

protectedvoidonActivityResult(intrequestCode, intresultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     try{ 
         Bundle extras = data.getExtras(); 
         Bitmap b = (Bitmap) extras.get("data"); 
         take = b; 
         ImageView img = (ImageView)findViewById(R.id.image); 
         img.setImageBitmap(take); 
     }catch(Exception e){ 
     } 

但是这样你会发现这个bitmap尺寸太小了。明显是被压缩过了,要像返回未被压缩的照片,那么你要给调用系统拍照程序intent加上参数,指定图片输出的位置。 




Intent it = newIntent("android.media.action.IMAGE_CAPTURE"); 
it.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(newFile(F.SD_CARD_TEMP_PHOTO_PATH))); 
startActivityForResult(it, Activity.DEFAULT_KEYS_DIALER); 
这样就是大图片返回了。 

protectedvoidonActivityResult(intrequestCode, intresultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     try{ 
         ImageView img = (ImageView)findViewById(R.id.image); 
         take = U.ResizeBitmap(U.getBitmapForFile(F.SD_CARD_TEMP_PHOTO_PATH), 640); 
         img.setImageBitmap(take); 
         imgflag = true; 
     }catch(Exception e){ 
     } 

另外注意一下,返回的那个bitmap会很大,你用完以后要把它回收掉,不然你很容易内存报oom错误 

publicstaticBitmap ResizeBitmap(Bitmap bitmap, intnewWidth) { 
     intwidth = bitmap.getWidth(); 
     intheight = bitmap.getHeight(); 
     floattemp = ((float) height) / ((float) width); 
     intnewHeight = (int) ((newWidth) * temp); 
     floatscaleWidth = ((float) newWidth) / width; 
     floatscaleHeight = ((float) newHeight) / height; 
     Matrix matrix = newMatrix(); 
     // resize the bit map 
     matrix.postScale(scaleWidth, scaleHeight); 
     // matrix.postRotate(45); 
     Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); 
     bitmap.recycle(); 
     returnresizedBitmap; 
}

完整版 


java类代码: 

Java代码   收藏代码
  1. package com.android.camera;  
  2.   
  3. import java.io.File;  
  4. import java.io.InputStreamReader;  
  5.   
  6. import com.android.R;  
  7. import com.utils.CommonUtil;  
  8. import com.utils.ExceptionDetail;  
  9.   
  10. import android.app.Activity;  
  11. import android.content.Intent;  
  12. import android.graphics.Bitmap;  
  13. import android.graphics.BitmapFactory;  
  14. import android.graphics.Matrix;  
  15. import android.net.Uri;  
  16. import android.os.Bundle;  
  17. import android.os.Environment;  
  18. import android.provider.MediaStore;  
  19. import android.view.View;  
  20. import android.view.View.OnClickListener;  
  21. import android.widget.Button;  
  22. import android.widget.ImageView;  
  23.   
  24. /** 
  25.  * 调用系统相机拍照保存图片并浏览图片 
  26.  * 
  27.  */  
  28. public class CameraTest8 extends Activity implements OnClickListener{  
  29.       
  30.     private String PHOTO_FOLDER=new File(Environment.getExternalStorageDirectory(), "").getPath()+"/myAndroid/";  
  31.     private String PHOTO_NAME="";  
  32.       
  33.     private ImageView imageView_pic;  
  34.   
  35.     @Override  
  36.     protected void onCreate(Bundle savedInstanceState) {  
  37.         // TODO Auto-generated method stub  
  38.         super.onCreate(savedInstanceState);  
  39.           
  40.         setContentView(R.layout.cameratest8);  
  41.           
  42.         File file=new File(PHOTO_FOLDER);  
  43.         if (!file.exists() && !file.isDirectory()) {  
  44.             file.mkdir();  
  45.         }  
  46.           
  47.         Button button_camera=(Button)findViewById(R.id.button_camera);  
  48.         button_camera.setOnClickListener(this);  
  49.         imageView_pic=(ImageView)findViewById(R.id.imageView_pic);  
  50.           
  51.     }  
  52.   
  53.     @Override  
  54.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  55.         // TODO Auto-generated method stub  
  56.         super.onActivityResult(requestCode, resultCode, data);  
  57.           
  58.         switch(requestCode){  
  59.         case Activity.DEFAULT_KEYS_DIALER:{  
  60.             try{   
  61.                  Bitmap take = resizeBitmap(getBitmapForFile(PHOTO_FOLDER+PHOTO_NAME), 640);   
  62.                  imageView_pic.setImageBitmap(take);  
  63.              }catch(Exception e){  
  64.                  System.out.println(ExceptionDetail.getErrorMessage(e));  
  65.              }   
  66.   
  67.             break;  
  68.         }  
  69.         }  
  70.     }  
  71.       
  72.     public static Bitmap resizeBitmap(Bitmap bitmap, int newWidth) {   
  73.          int width = bitmap.getWidth();   
  74.          int height = bitmap.getHeight();   
  75.          float temp = ((float) height) / ((float) width);   
  76.          int newHeight = (int) ((newWidth) * temp);   
  77.          float scaleWidth = ((float) newWidth) / width;   
  78.          float scaleHeight = ((float) newHeight) / height;   
  79.          Matrix matrix = new Matrix();   
  80.          // resize the bit map   
  81.          matrix.postScale(scaleWidth, scaleHeight);   
  82.          // matrix.postRotate(45);   
  83.          Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 00, width, height, matrix, true);   
  84.          bitmap.recycle();   
  85.          return resizedBitmap;   
  86.     }  
  87.   
  88.     public static Bitmap getBitmapForFile(String filePath){  
  89.         Bitmap bitmap = BitmapFactory.decodeFile(filePath);  
  90.         return bitmap;  
  91.     }  
  92.   
  93.     @Override  
  94.     public void onClick(View v) {  
  95.         switch(v.getId()){  
  96.         case R.id.button_camera:{  
  97.             PHOTO_NAME="MyPic"+CommonUtil.getCurrentTime("yyyyMMddHHmmssSSS")+".jpg";  
  98.             Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);   
  99.             intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(PHOTO_FOLDER+PHOTO_NAME)));   
  100.             startActivityForResult(intent, Activity.DEFAULT_KEYS_DIALER);   
  101.             break;  
  102.         }  
  103.         }  
  104.     }  
  105.   
  106.   
  107. }  


布局文件代码: 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <Button  
  7.         android:id="@+id/button_camera"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_alignParentLeft="true"  
  11.         android:layout_alignParentTop="true"  
  12.         android:text="相机" />  
  13.   
  14.     <ImageView  
  15.         android:id="@+id/imageView_pic"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_below="@+id/button_camera"  
  19.         android:layout_centerHorizontal="true"  
  20.         android:layout_marginTop="60dp"  
  21.         android:src="@drawable/app_panel_friendcard_icon" />  
  22.   
  23. </RelativeLayout>  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值