1.Dialog布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:gravity="center" android:minWidth="180dp" android:orientation="vertical" android:padding="10dp" > <Button android:id="@+id/from_camera" style="@style/TextStyleFlowTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/ic_item_selector" android:padding="12dp" android:text="拍照" /> <View android:layout_width="match_parent" android:layout_height="1px" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:background="@color/divider" /> <Button android:id="@+id/from_gallery" style="@style/TextStyleFlowTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/ic_item_selector" android:padding="12dp" android:text="从相册中选择" /> </LinearLayout>
2.Dialog方法
/** * 选择头像设置是从拍照还是相册中选 */ private void showDialog() { final Dialog dialog = new Dialog(this, R.style.dialog); LayoutInflater inflater = LayoutInflater.from(this); View view = inflater.inflate(R.layout.view_photoselect, null); dialog.setContentView(view); // 拍照 view.findViewById(R.id.from_camera).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View arg0) { formCamera(); dialog.dismiss(); } }); // 从相册中选择 view.findViewById(R.id.from_gallery).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View arg0) { formGallerya(); dialog.dismiss(); } }); dialog.show(); } /** * 拍照 */ private void formCamera() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); tmp = System.currentTimeMillis() + ".jpg"; File file = new File(Utils.SD_IMAGES_PATH + tmp); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); startActivityForResult(intent, CODE_CAPTURE); } /** * 照片中选择 */ private void formGallerya() { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(intent, CODE_GALLERY); }
3.回调处理
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // 拍照返回图片 if (requestCode == CODE_CAPTURE) { if (resultCode == RESULT_OK) { mPhoto = Utils.SD_IMAGES_PATH + tmp; mPhotoHandler.sendMessage(mPhotoHandler.obtainMessage(0, "保存照片到" + mPhoto)); } else { showToast("拍照失败,请选择相册", Toast.LENGTH_SHORT); } } if (requestCode == CODE_GALLERY) { if (resultCode == RESULT_OK) { String path = dealPhotoFile(data); if (Utils.isEmpty(path)) { showToast("获取图片失败,请选择拍照", Toast.LENGTH_SHORT); } else { mPhoto = path; mPhotoHandler.sendMessage(mPhotoHandler.obtainMessage(0, "获取图片" + mPhoto)); } } else { showToast("获取图片失败,请选择拍照", Toast.LENGTH_SHORT); } } } private String dealPhotoFile(final Intent data) { String path = null; if (data != null) { Uri uri = data.getData(); if (uri != null) { path = uri.getPath(); if (new File(path).exists()) { return path; } } String[] proj = { MediaStore.Images.Media.DATA }; Cursor actualimagecursor = TaskDetailActivity.this.managedQuery( uri, proj, null, null, null); if (actualimagecursor != null) { int actual_image_column_index = actualimagecursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); if (actualimagecursor.moveToFirst()) { path = actualimagecursor .getString(actual_image_column_index); if (new File(path).exists()) { return path; } } } } return path; }
PS:
在创建bitmap过程中,某些手机会导致创建失败,图片过大
BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.RGB_565; options.inPurgeable = true; options.inInputShareable = true; options.inJustDecodeBounds = false; bmp = BitmapFactory.decodeFile(localImagePath, options);
public static Bitmap getBitmapFromFile(File dst, int width, int height) { if (null != dst && dst.exists()) { BitmapFactory.Options opts = null; opts = new BitmapFactory.Options(); if (width > 0 && height > 0) { opts.inJustDecodeBounds = true; BitmapFactory.decodeFile(dst.getPath(), opts); // 计算图片缩放比例 final int minSideLength = Math.min(width, height); opts.inSampleSize = computeSampleSize(opts, minSideLength, width * height); } opts.inPreferredConfig = Bitmap.Config.RGB_565; opts.inJustDecodeBounds = false; opts.inInputShareable = true; opts.inPurgeable = true; try { return BitmapFactory.decodeFile(dst.getPath(), opts); } catch (Exception e) { e.printStackTrace(); } catch (OutOfMemoryError e) { e.printStackTrace(); } } return null; } public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) { int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels); int roundedSize; if (initialSize <= 8) { roundedSize = 1; while (roundedSize < initialSize) { roundedSize <<= 1; } } else { roundedSize = (initialSize + 7) / 8 * 8; } return roundedSize; } private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) { double w = options.outWidth; double h = options.outHeight; int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math .sqrt(w * h / maxNumOfPixels)); int upperBound = (minSideLength == -1) ? 128 : (int) Math.min( Math.floor(w / minSideLength), Math.floor(h / minSideLength)); if (upperBound < lowerBound) { // return the larger one when there is no overlapping zone. return lowerBound; } if ((maxNumOfPixels == -1) && (minSideLength == -1)) { return 1; } else if (minSideLength == -1) { return lowerBound; } else { return upperBound; } }