android拍照和选择图片剪裁
一 :底部弹出的activity http://www.jb51.net/article/56990.htm
public class SelectPicActivity extends Activity implements OnClickListener { /* 头像名称 */ private static final String PHOTO_FILE_NAME = "temp_photo.jpg"; //拍照后的名称 /*** * 使用照相机拍照获取图片 */ public static final int PHOTO_REQUEST_CAMERA = 1; /*** * 使用相册中的图片 */ public static final int PHOTO_REQUEST_GALLERY = 2; // 剪切的结果 public static final int PHOTO_REQUEST_CUT = 3; /*** * 从Intent获取图片路径的KEY */ public static final String KEY_PHOTO_PATH = "photo_path"; private LinearLayout dialogLayout; private Button takePhotoBtn, pickPhotoBtn, cancelBtn; private Intent lastIntent; private File tempFile; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.select_pic_layout); tempFile = new File(Environment.getExternalStorageDirectory(), getPhotoFileName()); initView(); } /** * 初始化加载View */ private void initView() { dialogLayout = (LinearLayout) findViewById(R.id.dialog_layout); dialogLayout.setOnClickListener(this); takePhotoBtn = (Button) findViewById(R.id.btn_take_photo); takePhotoBtn.setOnClickListener(this); pickPhotoBtn = (Button) findViewById(R.id.btn_pick_photo); pickPhotoBtn.setOnClickListener(this); cancelBtn = (Button) findViewById(R.id.btn_cancel); cancelBtn.setOnClickListener(this); lastIntent = getIntent(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.dialog_layout: finish(); break; case R.id.btn_take_photo: camera(); break; case R.id.btn_pick_photo: gallery(); break; default: finish(); break; } } /* * 从相册获取 */ public void gallery() { // 激活系统图库,选择一张图片 ,一般用 ACTION_GET_CONTENT Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/*"); startActivityForResult(intent, PHOTO_REQUEST_GALLERY); } /* * 从相机获取 */ File tempCameraFile = null; public void camera() { // 判断存储卡是否可以用,可用进行存储 if (hasSdcard()) { tempCameraFile = new File(Environment.getExternalStorageDirectory(), PHOTO_FILE_NAME); Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempCameraFile)); startActivityForResult(intent, PHOTO_REQUEST_CAMERA); }else { Toast.makeText(getApplicationContext(), "无内存卡~", 1).show(); } } @Override public boolean onTouchEvent(MotionEvent event) { finish(); return super.onTouchEvent(event); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PHOTO_REQUEST_GALLERY) { //相册中获取 if (data != null) { // 得到图片的全路径 Uri mUri= data.getData(); crop(mUri); } } else if (requestCode == PHOTO_REQUEST_CAMERA && data==null) { //照相中获取 if (hasSdcard()) { Uri uri = Uri.fromFile(tempCameraFile); crop(uri); } else { Toast.makeText(SelectPicActivity.this, "未找到存储卡,无法存储照片!", 0).show(); } } else if (requestCode == PHOTO_REQUEST_CUT){ try { String absolutePath = tempFile.getAbsolutePath(); lastIntent.putExtra(KEY_PHOTO_PATH, absolutePath); setResult(-2, lastIntent); //保存路径返回 finish(); tempCameraFile.delete(); //删除拍照后的文件 } catch (Exception e) { e.printStackTrace(); } } super.onActivityResult(requestCode, resultCode, data); } /** * 裁剪图片方法实现 * * @param uri */ public void crop(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // 设置裁剪 intent.putExtra("crop", "true"); intent.putExtra("output", Uri.fromFile(tempFile)); // aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY 是裁剪图片宽高 intent.putExtra("outputX", 320); intent.putExtra("outputY", 320); intent.putExtra("outputFormat", "JPEG");// 图片格式 intent.putExtra("return-data", false); //设置为false ,有的手机会报错 startActivityForResult(intent, PHOTO_REQUEST_CUT); } // 使用系统当前日期加以调整作为照片的名称 private String getPhotoFileName() { Date date = new Date(System.currentTimeMillis()); SimpleDateFormat dateFormat = new SimpleDateFormat( "'IMG'_yyyyMMdd_HHmmss"); return dateFormat.format(date) + ".jpg"; } private boolean hasSdcard() { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { return true; } else { return false; } } }
这个activity 的样式
<style name="DialogStyleBottom" parent="android:Theme.Dialog"> <item name="android:windowAnimationStyle">@style/AnimBottom</item> <item name="android:windowFrame">@null</item> <!-- 边框 --> <item name="android:windowIsFloating">false</item> <!-- 是否浮现在activity之上 --> <item name="android:windowIsTranslucent">true</item> <!-- 半透明 --> <item name="android:windowNoTitle">true</item> <!-- 无标题 --> <item name="android:windowBackground">@android:color/transparent</item> <!-- 背景透明 --> <item name="android:backgroundDimEnabled">true</item> <!-- 模糊 --> </style>
<style name="AnimBottom" parent="@android:style/Animation">
<item name="android:windowEnterAnimation">@anim/push_bottom_in</item>
<item name="android:windowExitAnimation">@anim/push_bottom_out</item>
</style>
anim/push_bottom_in
<?xml version="1.0" encoding="utf-8"?> <!-- 上下滑入式 --> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="200" android:fromYDelta="100%p" android:toYDelta="0" /> </set>
push_bottom_out
<?xml version="1.0" encoding="utf-8"?> <!-- 上下滑出式 --> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="200" android:fromYDelta="0" android:toYDelta="50%p" /> </set>
主界面的activity
public class MainActivity extends Activity implements OnClickListener{ private static final String TAG = "uploadImage"; /** * 选择文件 */ public static final int TO_SELECT_PHOTO = 3; private Button selectButton,uploadButton; private ImageView imageView; private String picPath = null; //返回來图片的的路径 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initView(); } /** * 初始化数据 */ private void initView() { selectButton = (Button) this.findViewById(R.id.selectImage); uploadButton = (Button) this.findViewById(R.id.uploadImage); selectButton.setOnClickListener(this); uploadButton.setOnClickListener(this); imageView = (ImageView) this.findViewById(R.id.imageView); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.selectImage: Intent intent = new Intent(this,SelectPicActivity.class); startActivityForResult(intent, TO_SELECT_PHOTO); //跳转到拍照和 选择图片 break; default: break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if( requestCode == TO_SELECT_PHOTO && data!=null){ //返回来的图片的路径 picPath = data.getStringExtra(SelectPicActivity.KEY_PHOTO_PATH); Bitmap bm = BitmapFactory.decodeFile(picPath); imageView.setImageBitmap(bm); } super.onActivityResult(requestCode, resultCode, data); } }