分享

android调用摄像头拍照,从相册中选择照片并裁剪

 quasiceo 2016-01-09
2015-10-30 18:24 859人阅读 评论(0) 收藏 举报
分类:

版权声明:本文为博主许佳佳原创文章,转载请务必注明出处。

在此知识点上笔者也是花了大量的时间,由于android版本的更新,android4.4和android4.3在OnActivityResult中返回的uri是完全不同的,所以也造成了笔者的学习资料中的demo不能正常运行的情况,也是纠结了大半天。

但是在此博文中也是对其中细节不做介绍了,还是把正确的代码展示给大家把。


主界面:





选择相册中的照片:





选择照片后的裁剪界面:





完成后效果:




MainActivity:

  1. import java.io.File;  
  2. import java.io.FileNotFoundException;  
  3. import java.io.IOException;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.graphics.Bitmap;  
  8. import android.graphics.BitmapFactory;  
  9. import android.net.Uri;  
  10. import android.os.Bundle;  
  11. import android.os.Environment;  
  12. import android.provider.MediaStore;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.widget.Button;  
  16. import android.widget.ImageView;  
  17. import android.widget.Toast;  
  18.   
  19. public class MainActivity extends Activity {  
  20.   
  21.     public static final int CUT_PICTURE = 1;  
  22.   
  23.     public static final int SHOW_PICTURE = 2;  
  24.   
  25.     private Button takePhoto;  
  26.   
  27.     private Button chooseFromAlbum;  
  28.   
  29.     private ImageView picture;  
  30.   
  31.     private Uri imageUri;  
  32.   
  33.     @Override  
  34.     protected void onCreate(Bundle savedInstanceState) {  
  35.         super.onCreate(savedInstanceState);  
  36.         setContentView(R.layout.activity_main);  
  37.         takePhoto = (Button) findViewById(R.id.take_photo);  
  38.         chooseFromAlbum = (Button) findViewById(R.id.choose_from_album);  
  39.         picture = (ImageView) findViewById(R.id.picture);  
  40.   
  41.         takePhoto.setOnClickListener(new OnClickListener() {  
  42.             @Override  
  43.             public void onClick(View v) {  
  44.                 //创建File对象,用于存储拍照后的图片  
  45.                 //将此图片存储于SD卡的根目录下  
  46.                 File outputImage = new File(Environment.getExternalStorageDirectory(),  
  47.                         "output_image.jpg");  
  48.                 try {  
  49.                     if (outputImage.exists()) {  
  50.                         outputImage.delete();  
  51.                     }  
  52.                     outputImage.createNewFile();  
  53.                 } catch (IOException e) {  
  54.                     e.printStackTrace();  
  55.                 }  
  56.                 //将File对象转换成Uri对象  
  57.                 //Uri表标识着图片的地址  
  58.                 imageUri = Uri.fromFile(outputImage);  
  59.                 //隐式调用照相机程序  
  60.                 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");  
  61.                 //拍下的照片会被输出到output_image.jpg中去  
  62.                 intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);  
  63.                 //此处是使用的startActivityForResult()  
  64.                 //因此在拍照完后悔有结果返回到onActivityResult()中去,返回值即为<span style="font-size: 13.3333px; font-family: Arial, Helvetica, sans-serif;">CUT_PICTURE</span>  
  65.                 //onActivityResult()中主要是实现图片裁剪  
  66.                 startActivityForResult(intent, CUT_PICTURE);  
  67.             }  
  68.         });  
  69.   
  70.         chooseFromAlbum.setOnClickListener(new OnClickListener() {  
  71.             @Override  
  72.             public void onClick(View v) {  
  73.                 File outputImage = new File(Environment.getExternalStorageDirectory(),  
  74.                         "output_image.jpg");  
  75.                 try {  
  76.                     if (outputImage.exists()) {  
  77.                         outputImage.delete();  
  78.                     }  
  79.                     outputImage.createNewFile();  
  80.                 } catch (IOException e) {  
  81.                     e.printStackTrace();  
  82.                 }  
  83.                 imageUri = Uri.fromFile(outputImage);  
  84.                 Intent intent = new Intent(Intent.ACTION_PICK,null);  
  85.                 //此处调用了图片选择器  
  86.                 //如果直接写intent.setDataAndType("image/*");  
  87.                 //调用的是系统图库  
  88.                 intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");  
  89.                 intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);  
  90.                 startActivityForResult(intent, CUT_PICTURE);  
  91.             }  
  92.         });  
  93.     }  
  94.   
  95.   
  96.     @Override  
  97.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  98.         switch (requestCode) {  
  99.         case CUT_PICTURE:  
  100.             if (resultCode == RESULT_OK) {  
  101.                 //此处启动裁剪程序  
  102.                 Intent intent = new Intent("com.android.camera.action.CROP");  
  103.                 //此处注释掉的部分是针对android 4.4路径修改的一个测试  
  104.                 //有兴趣的读者可以自己调试看看  
  105. //              String text=data.getData().toString();  
  106. //              Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();  
  107.                 intent.setDataAndType(data.getData(), "image/*");  
  108.                 intent.putExtra("scale"true);  
  109.                 intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);  
  110.                 startActivityForResult(intent, SHOW_PICTURE);  
  111.             }  
  112.             break;  
  113.         case SHOW_PICTURE:  
  114.             if (resultCode == RESULT_OK) {  
  115.                 try {  
  116.                     //将output_image.jpg对象解析成Bitmap对象,然后设置到ImageView中显示出来  
  117.                     Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()  
  118.                             .openInputStream(imageUri));  
  119.                     picture.setImageBitmap(bitmap);  
  120.                 } catch (FileNotFoundException e) {  
  121.                     e.printStackTrace();  
  122.                 }  
  123.             }  
  124.             break;  
  125.         default:  
  126.             break;  
  127.         }  
  128.     }  
  129.   
  130.   
  131.   
  132. }  



activity_main:

  1. <LinearLayout xmlns:android="http://schemas./apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:orientation="vertical" >  
  5.   
  6.     <Button  
  7.         android:id="@+id/take_photo"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="Take Photo" />  
  11.   
  12.     <Button  
  13.         android:id="@+id/choose_from_album"  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="Choose From Album" />  
  17.   
  18.     <ImageView  
  19.         android:id="@+id/picture"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_gravity="center_horizontal" />  
  23.   
  24. </LinearLayout>  



AndroidManifest:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas./apk/res/android"  
  3.     package="com.example.choosepictest"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  8.   
  9.     <uses-sdk  
  10.         android:minSdkVersion="14"  
  11.         android:targetSdkVersion="17" />  
  12.   
  13.     <application  
  14.         android:allowBackup="true"  
  15.         android:icon="@drawable/ic_launcher"  
  16.         android:label="@string/app_name"  
  17.         android:theme="@style/AppTheme" >  
  18.         <activity  
  19.             android:name="com.example.choosepictest.MainActivity"  
  20.             android:label="@string/app_name" >  
  21.             <intent-filter>  
  22.                 <action android:name="android.intent.action.MAIN" />  
  23.   
  24.                 <category android:name="android.intent.category.LAUNCHER" />  
  25.             </intent-filter>  
  26.         </activity>  
  27.     </application>  
  28.   
  29. </manifest>  

1
0

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约