转载请注明出处:http://blog.csdn.net/loveyaozu/article/details/51160482

相信有很多Android开发人员在日常开发中,由于项目需求,需要我们的APP能够从相册中选取图片并剪辑,以及拍照剪辑后上传的功能。如果之前你没有做过这个功能,刚开始做的时候可能会遇到一些列的问题,这些问题大多是细节上的问题。今天,就根据自己的开发经验,给大家提供一套完成的相册图片选取剪辑和拍照剪辑的代码事例。我提供的代码可能还会存在一些问题,大家可以相互交流学习。

这里会给大家提供一些我整理的一些图片处理的工具类,供大家使用。

拍照,相册图片选取以及图片剪辑的工具类,PhotoUtils.java

package com.syz.photograph;
import java.io.File;
import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.Date;import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;public class PhotoUtil {public static final int NONE = 0;public static final String IMAGE_UNSPECIFIED = "image/*";//任意图片类型public static final int PHOTOGRAPH = 1;// 拍照public static final int PHOTOZOOM = 2; // 缩放public static final int PHOTORESOULT = 3;// 结果public static final int PICTURE_HEIGHT = 500;public static final int PICTURE_WIDTH = 500;public static String imageName;/*** 从系统相册中选取照片上传* @param activity*/public static void selectPictureFromAlbum(Activity activity){// 调用系统的相册Intent intent = new Intent(Intent.ACTION_PICK, null);intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,IMAGE_UNSPECIFIED);// 调用剪切功能activity.startActivityForResult(intent, PHOTOZOOM);}/*** 从系统相册中选取照片上传* @param fragment*/public static void selectPictureFromAlbum(Fragment fragment){// 调用系统的相册Intent intent = new Intent(Intent.ACTION_PICK, null);intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,IMAGE_UNSPECIFIED);// 调用剪切功能fragment.startActivityForResult(intent, PHOTOZOOM);}/*** 拍照* @param activity*/public static void photograph(Activity activity){imageName = File.separator + getStringToday() + ".jpg";// 调用系统的拍照功能Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);String status = Environment.getExternalStorageState();if(status.equals(Environment.MEDIA_MOUNTED)){intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), imageName)));}else{intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(activity.getFilesDir(), imageName)));}activity.startActivityForResult(intent, PHOTOGRAPH);}/*** 拍照* @param fragment*/public static void photograph(Fragment fragment){imageName = "/" + getStringToday() + ".jpg";// 调用系统的拍照功能Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);String status = Environment.getExternalStorageState();if(status.equals(Environment.MEDIA_MOUNTED)){intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), imageName)));}else{intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(fragment.getActivity().getFilesDir(), imageName)));}fragment.startActivityForResult(intent, PHOTOGRAPH);}/*** 图片裁剪* @param activity* @param uri* @param height* @param width*/public static void startPhotoZoom(Activity activity,Uri uri,int height,int width) {Intent intent = new Intent("com.android.camera.action.CROP");intent.setDataAndType(uri, IMAGE_UNSPECIFIED);intent.putExtra("crop", "true");// aspectX aspectY 是宽高的比例intent.putExtra("aspectX", 1);intent.putExtra("aspectY", 1);// outputX outputY 是裁剪图片宽高intent.putExtra("outputX", height);intent.putExtra("outputY", width);intent.putExtra("noFaceDetection", true); //关闭人脸检测intent.putExtra("return-data", true);//如果设为true则返回bitmapintent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//输出文件intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());activity.startActivityForResult(intent, PHOTORESOULT);}/*** 图片裁剪* @param activity  * @param uri       原图的地址* @param height   指定的剪辑图片的高* @param width        指定的剪辑图片的宽* @param destUri  剪辑后的图片存放地址*/public static void startPhotoZoom(Activity activity,Uri uri,int height,int width,Uri destUri) {Intent intent = new Intent("com.android.camera.action.CROP");intent.setDataAndType(uri, IMAGE_UNSPECIFIED);intent.putExtra("crop", "true");// aspectX aspectY 是宽高的比例intent.putExtra("aspectX", 1);intent.putExtra("aspectY", 1);// outputX outputY 是裁剪图片宽高intent.putExtra("outputX", height);intent.putExtra("outputY", width);intent.putExtra("noFaceDetection", true); //关闭人脸检测intent.putExtra("return-data", false);//如果设为true则返回bitmapintent.putExtra(MediaStore.EXTRA_OUTPUT, destUri);//输出文件intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());activity.startActivityForResult(intent, PHOTORESOULT);}/*** 图片裁剪* @param fragment* @param uri* @param height* @param width*/public static void startPhotoZoom(Fragment fragment,Uri uri,int height,int width) {Intent intent = new Intent("com.android.camera.action.CROP");intent.setDataAndType(uri, IMAGE_UNSPECIFIED);intent.putExtra("crop", "true");// aspectX aspectY 是宽高的比例intent.putExtra("aspectX", 1);intent.putExtra("aspectY", 1);// outputX outputY 是裁剪图片宽高intent.putExtra("outputX", height);intent.putExtra("outputY", width);intent.putExtra("return-data", true);fragment.startActivityForResult(intent, PHOTORESOULT);}/*** 获取当前系统时间并格式化* @return*/@SuppressLint("SimpleDateFormat")public static String getStringToday() {Date currentTime = new Date();SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");String dateString = formatter.format(currentTime);return dateString;}/*** 制作图片的路径地址* @param context* @return*/public static String getPath(Context context){String path = null;File file = null;long tag = System.currentTimeMillis();if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){//SDCard是否可用path = Environment.getExternalStorageDirectory() + File.separator +"myimages/";file = new File(path);if(!file.exists()){file.mkdirs();}path = Environment.getExternalStorageDirectory() + File.separator +"myimages/"+ tag + ".png";}else{path = context.getFilesDir() + File.separator +"myimages/";file = new File(path);if(!file.exists()){file.mkdirs();}path = context.getFilesDir() + File.separator +"myimages/"+ tag + ".png";}return path;}/*** 按比例获取bitmap* @param path* @param w* @param h* @return*/public static Bitmap convertToBitmap(String path, int w, int h) {BitmapFactory.Options opts = new BitmapFactory.Options();// 设置为ture只获取图片大小opts.inJustDecodeBounds = true;opts.inPreferredConfig = Bitmap.Config.ARGB_8888;BitmapFactory.decodeFile(path, opts);int width = opts.outWidth;int height = opts.outHeight;float scaleWidth = 0.f, scaleHeight = 0.f;if (width > w || height > h) {// 缩放scaleWidth = ((float) width) / w;scaleHeight = ((float) height) / h;}opts.inJustDecodeBounds = false;float scale = Math.max(scaleWidth, scaleHeight);opts.inSampleSize = (int)scale;WeakReference<Bitmap> weak = new WeakReference<Bitmap>(BitmapFactory.decodeFile(path, opts));return Bitmap.createScaledBitmap(weak.get(), w, h, true);}/*** 获取原图bitmap* @param path* @return*/public static Bitmap convertToBitmap2(String path) {BitmapFactory.Options opts = new BitmapFactory.Options();// 设置为ture只获取图片大小opts.inJustDecodeBounds = true;opts.inPreferredConfig = Bitmap.Config.ARGB_8888;// 返回为空BitmapFactory.decodeFile(path, opts);return  BitmapFactory.decodeFile(path, opts);}
}

图片压缩等一些列图片处理的工具类 ImageUtils.java

package com.syz.photograph;import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
/*** 图片处理的工具类* @author yaozu**/
public class ImageUtils {private static final String TAG = ImageUtils.class.getSimpleName();/*** 根据Uri获取路径* @param contentUri* @return*/public static String getRealPathByURI(Uri contentUri,Context context) {String res = null;String[] proj = { MediaStore.Images.Media.DATA };Cursor cursor = context.getContentResolver().query(contentUri,proj, null, null, null);if (cursor.moveToFirst()) {;int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);res = cursor.getString(column_index);}cursor.close();return res;}/*** 创建一条图片地址uri,用于保存拍照后的照片* * @param context* @return 图片的uri*/public static Uri createImagePathUri(Context context) {Uri imageFilePath = null;String status = Environment.getExternalStorageState();SimpleDateFormat timeFormatter = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA);long time = System.currentTimeMillis();String imageName = timeFormatter.format(new Date(time));// ContentValues是我们希望这条记录被创建时包含的数据信息ContentValues values = new ContentValues(3);values.put(MediaStore.Images.Media.DISPLAY_NAME, imageName);values.put(MediaStore.Images.Media.DATE_TAKEN, time);values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");if (status.equals(Environment.MEDIA_MOUNTED)) {// 判断是否有SD卡,优先使用SD卡存储,当没有SD卡时使用手机存储imageFilePath = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);} else {imageFilePath = context.getContentResolver().insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);}Log.i("", "生成的照片输出路径:" + imageFilePath.toString());return imageFilePath;}/*** 图片压缩* * @param bmp* @param file*/public static void compressBmpToFile(File file,int height,int width) {Bitmap bmp = decodeSampledBitmapFromFile(file.getPath(), height, width);ByteArrayOutputStream baos = new ByteArrayOutputStream();int options = 100;bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);/*while (baos.toByteArray().length / 1024 > 30) {baos.reset();if (options - 10 > 0) {options = options - 10;bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);}if (options - 10 <= 0) {break;}}*/try {FileOutputStream fos = new FileOutputStream(file);fos.write(baos.toByteArray());fos.flush();fos.close();} catch (Exception e) {e.printStackTrace();}}/*** 将图片变成bitmap* * @param path* @return*/public static Bitmap getImageBitmap(String path) {Bitmap bitmap = null;File file = new File(path);if (file.exists()) {bitmap = BitmapFactory.decodeFile(path);return bitmap;}return null;}//=================================图片压缩方法===============================================/*** 质量压缩* @author ping 2015-1-5 下午1:29:58* @param image* @param maxkb* @return*/public static Bitmap compressBitmap(Bitmap image,int maxkb) {//L.showlog(压缩图片);ByteArrayOutputStream baos = new ByteArrayOutputStream();// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中image.compress(Bitmap.CompressFormat.JPEG, 50, baos);int options = 100;// 循环判断如果压缩后图片是否大于(maxkb)50kb,大于继续压缩while (baos.toByteArray().length / 1024 > maxkb) { // 重置baos即清空baosbaos.reset();if(options-10>0){// 每次都减少10options -= 10;}// 这里压缩options%,把压缩后的数据存放到baos中image.compress(Bitmap.CompressFormat.JPEG, options, baos);}// 把压缩后的数据baos存放到ByteArrayInputStream中ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把ByteArrayInputStream数据生成图片Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);return bitmap;}/*** * @param res* @param resId* @param reqWidth*            所需图片压缩尺寸最小宽度* @param reqHeight*            所需图片压缩尺寸最小高度* @return*/public static Bitmap decodeSampledBitmapFromResource(Resources res,int resId, int reqWidth, int reqHeight) {final BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeResource(res, resId, options);options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);options.inJustDecodeBounds = false;return BitmapFactory.decodeResource(res, resId, options);}/*** * @param filepath*             图片路径* @param reqWidth*            所需图片压缩尺寸最小宽度* @param reqHeight*          所需图片压缩尺寸最小高度* @return*/public static Bitmap decodeSampledBitmapFromFile(String filepath,int reqWidth, int reqHeight) {final BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeFile(filepath, options);options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);options.inJustDecodeBounds = false;return BitmapFactory.decodeFile(filepath, options);}/*** * @param bitmap* @param reqWidth*          所需图片压缩尺寸最小宽度* @param reqHeight*            所需图片压缩尺寸最小高度* @return*/public static Bitmap decodeSampledBitmapFromBitmap(Bitmap bitmap,int reqWidth, int reqHeight) {ByteArrayOutputStream baos = new ByteArrayOutputStream();bitmap.compress(Bitmap.CompressFormat.PNG, 90, baos);byte[] data = baos.toByteArray();final BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeByteArray(data, 0, data.length, options);options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);options.inJustDecodeBounds = false;return BitmapFactory.decodeByteArray(data, 0, data.length, options);}/*** 计算压缩比例值(改进版 by touch_ping)* * 原版2>4>8...倍压缩* 当前2>3>4...倍压缩* * @param options*            解析图片的配置信息* @param reqWidth*            所需图片压缩尺寸最小宽度O* @param reqHeight*            所需图片压缩尺寸最小高度* @return*/public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {final int picheight = options.outHeight;final int picwidth = options.outWidth;int targetheight = picheight;int targetwidth = picwidth;int inSampleSize = 1;if (targetheight > reqHeight || targetwidth > reqWidth) {while (targetheight  >= reqHeight&& targetwidth>= reqWidth) {inSampleSize += 1;targetheight = picheight/inSampleSize;targetwidth = picwidth/inSampleSize;}}Log.i("===","最终压缩比例:" +inSampleSize + "倍");Log.i("===", "新尺寸:" +  targetwidth + "*" +targetheight);return inSampleSize;}// 读取图像的旋转度public static int readBitmapDegree(String path) {int degree = 0;try {ExifInterface exifInterface = new ExifInterface(path);int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL);switch (orientation) {case ExifInterface.ORIENTATION_ROTATE_90:degree = 90;break;case ExifInterface.ORIENTATION_ROTATE_180:degree = 180;break;case ExifInterface.ORIENTATION_ROTATE_270:degree = 270;break;}} catch (IOException e) {e.printStackTrace();}return degree;}/*** 将图片按照某个角度进行旋转** @param bm*            需要旋转的图片* @param degree*            旋转角度* @return 旋转后的图片*/public static Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {Bitmap returnBm = null;// 根据旋转角度,生成旋转矩阵Matrix matrix = new Matrix();matrix.postRotate(degree);try {// 将原始图片按照旋转矩阵进行旋转,并得到新的图片returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);} catch (OutOfMemoryError e) {}if (returnBm == null) {returnBm = bm;}if (bm != returnBm) {bm.recycle();}return returnBm;}/*** * @param mBitmap* @param fileName*/public static void saveBitmapToLocal(Bitmap mBitmap,String fileName) {if(mBitmap != null){FileOutputStream fos = null;try {File file = new File(fileName);if(file.exists()){file.delete();}file.createNewFile();fos = new FileOutputStream(file);mBitmap.compress(Bitmap.CompressFormat.PNG, 80, fos);fos.flush();} catch (Exception e) {e.printStackTrace();}finally{try {if(fos != null){fos.close();}} catch (IOException e) {e.printStackTrace();}}}}/*** 将下载下来的图片保存到SD卡或者本地.并返回图片的路径(包括文件命和扩展名)* @param context* @param bitName* @param mBitmap* @return*/public static String saveBitmap(Context context,String bitName, Bitmap mBitmap) {String path = null;File f;if(mBitmap != null){if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){f = new File(Environment.getExternalStorageDirectory() + File.separator +"images/");String fileName = Environment.getExternalStorageDirectory() + File.separator +"images/"+ bitName + ".png";path = fileName;FileOutputStream fos = null;try {if(!f.exists()){f.mkdirs();}File file = new File(fileName);file.createNewFile();fos = new FileOutputStream(file);mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);fos.flush();} catch (Exception e) {e.printStackTrace();}finally{try {if(fos != null){fos.close();}} catch (IOException e) {e.printStackTrace();}}}else{//本地存储路径f = new File(context.getFilesDir() + File.separator +"images/");Log.i(TAG, "本地存储路径:"+context.getFilesDir() + File.separator +"images/"+ bitName + ".png");path = context.getFilesDir() + File.separator +"images/"+ bitName + ".png";FileOutputStream fos = null;try {if(!f.exists()){f.mkdirs();}File file = new File(path);file.createNewFile();fos = new FileOutputStream(file);mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);fos.flush();} catch (IOException e) {e.printStackTrace();}finally{try {if(fos != null){fos.close();}} catch (IOException e) {e.printStackTrace();}}}}return path;}/*** 删除图片* @param context* @param bitName*/public void deleteFile(Context context,String bitName) {  if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){File dirFile = new File(Environment.getExternalStorageDirectory() + File.separator + "images/"+ bitName + ".png"); if (!dirFile.exists()) {return;}dirFile.delete();} else {File f = new File(context.getFilesDir() + File.separator+ "images/" + bitName + ".png");if(!f.exists()){return;}f.delete();}}}

这里给大家展示一个简单的拍照,相册图片选取,剪辑图片的例子demo。

首先创建activity_main.xml布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="${relativePackage}.${activityClass}" ><Buttonandroid:id="@+id/options"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="30dp"android:gravity="center"android:textSize="15sp"android:text="@string/options_choose" /><TextViewandroid:id="@+id/small_img_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="30dp"android:textSize="15sp"/><ImageViewandroid:id="@+id/small_img"android:layout_width="wrap_content"android:layout_height="wrap_content"android:contentDescription="@string/app_name" /><TextViewandroid:id="@+id/clip_pic_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="30dp"android:textSize="15sp"/><ImageViewandroid:id="@+id/clip_pic"android:layout_width="wrap_content"android:layout_height="wrap_content"android:contentDescription="@string/app_name" /></LinearLayout>

结合上面的PhotoUtils实现拍照,相册图片选取,图片剪辑功能。

MainActivity.java

package com.syz.photograph;import java.io.File;import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;import com.syz.photograph.ActionSheetDialog.OnSheetItemClickListener;
import com.syz.photograph.ActionSheetDialog.SheetItemColor;public class MainActivity extends Activity implements OnClickListener {private static final String TAG = MainActivity.class.getSimpleName();private ImageView smallImg;private ImageView clipImg;private TextView tv1,tv2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.options).setOnClickListener(this);initView();}protected void initView() {smallImg = (ImageView) findViewById(R.id.small_img);clipImg = (ImageView) findViewById(R.id.clip_pic);tv1 = (TextView) findViewById(R.id.small_img_title);tv2 = (TextView) findViewById(R.id.clip_pic_title);}@Overridepublic void onClick(View v) {if (v.getId() == R.id.options) {options();}}private String path;@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (resultCode == PhotoUtil.NONE)return;// 拍照if (requestCode == PhotoUtil.PHOTOGRAPH) {// 设置文件保存路径这里放在跟目录下File picture = null;if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {picture = new File(Environment.getExternalStorageDirectory() + PhotoUtil.imageName);if (!picture.exists()) {picture = new File(Environment.getExternalStorageDirectory() + PhotoUtil.imageName);}} else {picture = new File(this.getFilesDir() + PhotoUtil.imageName);if (!picture.exists()) {picture = new File(MainActivity.this.getFilesDir() + PhotoUtil.imageName);}}path = PhotoUtil.getPath(this);// 生成一个地址用于存放剪辑后的图片if (TextUtils.isEmpty(path)) {Log.e(TAG, "随机生成的用于存放剪辑后的图片的地址失败");return;}Uri imageUri = UriPathUtils.getUri(this, path);PhotoUtil.startPhotoZoom(MainActivity.this, Uri.fromFile(picture), PhotoUtil.PICTURE_HEIGHT, PhotoUtil.PICTURE_WIDTH, imageUri);}if (data == null)return;// 读取相册缩放图片if (requestCode == PhotoUtil.PHOTOZOOM) {path = PhotoUtil.getPath(this);// 生成一个地址用于存放剪辑后的图片if (TextUtils.isEmpty(path)) {Log.e(TAG, "随机生成的用于存放剪辑后的图片的地址失败");return;}Uri imageUri = UriPathUtils.getUri(this, path);PhotoUtil.startPhotoZoom(MainActivity.this, data.getData(), PhotoUtil.PICTURE_HEIGHT, PhotoUtil.PICTURE_WIDTH, imageUri);}// 处理结果if (requestCode == PhotoUtil.PHOTORESOULT) {/*** 在这里处理剪辑结果,可以获取缩略图,获取剪辑图片的地址。得到这些信息可以选则用于上传图片等等操作* *//*** 如,根据path获取剪辑后的图片*/Bitmap bitmap = PhotoUtil.convertToBitmap(path,PhotoUtil.PICTURE_HEIGHT, PhotoUtil.PICTURE_WIDTH);if(bitmap != null){tv2.setText(bitmap.getHeight()+"x"+bitmap.getWidth()+"图");clipImg.setImageBitmap(bitmap);}Bitmap bitmap2 = PhotoUtil.convertToBitmap(path,120, 120);if(bitmap2 != null){tv1.setText(bitmap2.getHeight()+"x"+bitmap2.getWidth()+"图");smallImg.setImageBitmap(bitmap2);}//            Bundle extras = data.getExtras();
//          if (extras != null) {
//              Bitmap photo = extras.getParcelable("data");
//              ByteArrayOutputStream stream = new ByteArrayOutputStream();
//              photo.compress(Bitmap.CompressFormat.JPEG, 100, stream);// (0-100)压缩文件
//              InputStream isBm = new ByteArrayInputStream(stream.toByteArray());
//          }}super.onActivityResult(requestCode, resultCode, data);}protected void options() {ActionSheetDialog mDialog = new ActionSheetDialog(this).builder();mDialog.setTitle("选择");mDialog.setCancelable(false);mDialog.addSheetItem("拍照", SheetItemColor.Blue, new OnSheetItemClickListener() {@Overridepublic void onClick(int which) {PhotoUtil.photograph(MainActivity.this);}}).addSheetItem("从相册选取", SheetItemColor.Blue, new OnSheetItemClickListener() {@Overridepublic void onClick(int which) {PhotoUtil.selectPictureFromAlbum(MainActivity.this);}}).show();}@Overrideprotected void onDestroy() {super.onDestroy();}}

效果图展示:

        

拍照+剪辑:

         

         

相册选取+剪辑:

 

最后附上Demo地址:http://download.csdn.net/detail/loveyaozu/9492321

Android之本地相册图片选取和拍照以及图片剪辑相关推荐

  1. Android获取本地相册图片

    Android获取本地相册图片 第一步设置静态权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_S ...

  2. Android进阶之路 - 解决部分手机拍照之后图片被旋转的问题

    这几天犯了一个错误,初期想着甩锅给后台的- 但还好及时发现了是自身的问题~ 关联文章 Android基础进阶 - 调用拍照.获取图片(基础) Android基础进阶 - 获取.调用相册内图片(基础) ...

  3. android开发 获取相册名称_android通过拍照、相册获取图片并显示 实例完整源码下载(亲测通过)...

    [实例简介]其中也包含了 将图片保存至 sd卡功能 [实例截图] [核心代码] public class MainActivity extends Activity{ private static f ...

  4. Android——获得本地相册(返回拍照照片)

    这里要实现类似微信选取相册的功能,其中呢,有两个功能: 1.拍照获得照片. 2.从相册中选取. 这里主界面就一个ImageView,通过获得照片来显示图片. 1.点击拍照后将调用本地摄像头拍照,并且将 ...

  5. Xamarin.Android 调用本地相册

    调用本地相册选中照片在ImageView上显示 代码: using System; using System.Collections.Generic; using System.Linq; using ...

  6. android 调用相机并获取图片地址,Android 7.0使用FileProvider获取相机拍照的图片路径...

    这里主要是基于Android 7.0,Nougat 实现一个获取相机拍照的图片后,使用FileProvider把图片转换为实际的路径. 首先需要在AndroidManifest.xml声明调用相机的权 ...

  7. Android获取本地相册中图片视频

    权限: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> < ...

  8. android从本地相册选择图片uri三星手机适配问题

    转载地址:http://blog.csdn.net/CathyChen0910/article/details/62456438 启动系统相册intent Intent intentFromGalle ...

  9. android从本地相册获取图片uri三星手机适配问题

    启动系统相册intent Intent intentFromGallery = new Intent(); if (android.os.Build.VERSION.SDK_INT >= and ...

最新文章

  1. 给gridview动态生成radiobutton添加OnCheckedChanged事件
  2. TF之CNN:利用sklearn(自带手写数字图片识别数据集)使用dropout解决学习中overfitting的问题+Tensorboard显示变化曲线
  3. 样条之贝塞尔(Bezier)
  4. 安装容器编排工具 Docker Compose
  5. mysqlnavicat数据库备份与恢复_Navicat如何还原MySQL数据库
  6. 论windows + asp.net性能
  7. 我端午节又来免费送书了!
  8. 黄聪:电子商务关键数字优化(线上部分,上)
  9. 移位运算符 实现 二进制数的 高低位翻转(完整逻辑代码)
  10. 西瓜书+实战+吴恩达机器学习(五)监督学习之线性判别分析 Linear Discriminant Analysis
  11. 遇到bug我会怎么做
  12. python 清华镜像_树莓派raspberry4B入坑指南 part-1 virtualenv安装python
  13. Ubuntu下两款划词翻译神器
  14. IDEA 插件开发 中文乱码
  15. 正切函数半角定理推导
  16. iOS:iOS开发非常全的三方库、插件等等
  17. 了解“黑马程序员”有感
  18. 未知地区的探索与猜想
  19. 网上百度的题目,随手写了一下
  20. 单核CPU与多核CPU的区别,多线程的优点,什么是并行?并发?

热门文章

  1. Shiro框架和JWT
  2. 支付宝支付设计和开发方案
  3. Java static关键字你了解多少?
  4. 大学计算机计算思维与网络数字考试,2018秋大学计算机计算思维导论(陇东学院)答案...
  5. scau 17967 大师姐唱K的固有结界 分类暴力 + RMQ
  6. Excel - VBA基础应用
  7. windows设置共享盘
  8. java面试题good and gbc
  9. 在 Windows 和 Mac 上将 M4A 转换为 WAV
  10. 装系统计算机丢失msi,msi电脑一键重装系统win7教程