标签:
一、最终成果
本例是用android自带的相册获取图片的,并且处理了很多异常,最终你的activity只需要用很少的代码就能获得用户选择好的图片了。
例子:
public class GetSimplePhotoHelperActivity extends KaleBaseActivity { GetSimplePhotoHelper mPhotoHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 去选择图片 mPhotoHelper = new GetSimplePhotoHelper(this); mPhotoHelper.choicePhoto(GetSimplePhotoHelper.FROM_WAY.FROM_ALBUM, null); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // 通过回调得到图片 ImageView view = getView(R.id.imageView); view.setImageBitmap(mPhotoHelper.getSelectedPhoto(resultCode, data).bitmap); } }
很简单吧,那么下面我们来看看如何实现这个功能。
二、GetSimplePhotoHelper
package com.kale.lib.photo; import com.kale.lib.utils.BitmapUtil; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import java.io.File; /** * @author Jack Tony * 从相册或者从照相机得到一个图片,没有裁剪功能 * <code> * @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { * super.onActivityResult(requestCode, resultCode, data); * imageView.setImageBitmap(task.getSelectedPhoto(resultCode, data)); * } * </code> * @date 2015/4/24 */ public class GetSimplePhotoHelper { public enum FROM_WAY { FROM_ALBUM, FROM_CAMERA; } private Activity mActivity; public static final int Get_PHOTO_RESULT_OK = 4603; private String mPicFilePath; private FROM_WAY mFromWay; public GetSimplePhotoHelper(Activity activity) { mActivity = activity; } /** * 从相册或照相机获得一张图片 * * @param way 获取图片的途径 * @param picFilePath 如果需要保存从相机拍摄的图片,请指定保存图片的全部路径(通过相机拍照时才有效) * eg:GetPhotoHelper.choicePhoto(GetPhotoHelper.FROM_WAY.FROM_CAMERA, Environment.getExternalStorageDirectory()+ "/temp.jpg"); */ public void choicePhoto(FROM_WAY way, String picFilePath) { mFromWay = way; mPicFilePath = picFilePath; if (way == FROM_WAY.FROM_ALBUM) { choicePhotoFromAlbum(); } else if (way == FROM_WAY.FROM_CAMERA) { choicePhotoFromCamera(picFilePath); } } /** * 启动相册的activity */ private void choicePhotoFromAlbum() { Intent intent = new Intent(mActivity, GetSimplePhotoActivity.class); intent.putExtra(GetSimplePhotoActivity.KEY_FROM_WAY, GetSimplePhotoActivity.VALUE_FROM_ALBUM); mActivity.startActivityForResult(intent, 0); } /** * 启动相机的activity */ private void choicePhotoFromCamera(String picFilePath) { Intent intent = new Intent(mActivity, GetSimplePhotoActivity.class); intent.putExtra(GetSimplePhotoActivity.KEY_FROM_WAY, GetSimplePhotoActivity.VALUE_FROM_CAMERA); intent.putExtra(GetSimplePhotoActivity.KEY_PHOTO_PATH, picFilePath); mActivity.startActivityForResult(intent, 0); } /** * 得到已经选择好的图片,这个方法必须在onActivityResult中进行回调 * * @return 已经选择好的bitmap */ public SimplePhoto getSelectedPhoto(int resultCode, Intent data) { SimplePhoto photo = new SimplePhoto(); if (resultCode == GetSimplePhotoHelper.Get_PHOTO_RESULT_OK) { Uri uri = data.getParcelableExtra(GetSimplePhotoActivity.KEY_CHOICE_PHOTO_OK_URI); //Logger.d("uri = " + uri); Bitmap bitmap = BitmapFactory.decodeFile(uri.toString()); // Logger.d("方向 =" + GetSimplePhotoUtil.getOrientation(uri)); if (bitmap != null && uri != null) { bitmap = BitmapUtil.rotateBitmap(bitmap, GetSimplePhotoUtil.getPhotoDegreeByUri(uri)); } photo.bitmap = bitmap; photo.uri = uri; photo.degree = GetSimplePhotoUtil.getPhotoDegreeByUri(uri); // 如果来源是相机,而且没有指定图片保存的目录,那么使用完毕后就立刻删除相片 if (mFromWay == FROM_WAY.FROM_CAMERA && mPicFilePath == null) { File tempPicFile = new File(uri.toString()); if (tempPicFile != null) { tempPicFile.delete();//设置成功后清除之前的照片文件 } } } return photo; } }
这个类主要是获得activity并且获得用户期望获取图片的途径,并且告诉GetSimplePhotoActivity(真正的处理类)。如果是想通过相机获取图片,那么这里还可以指定拍摄的照片保存的路径,不设置就不保存照片。最后,它还处理了照片返回的结果,判断照片的方向,进行正确的旋转产生最终的bitmap。总之,这个类做的是获得用户获取图片的途径,之后启动activity来进行处理。在处理结束后,它进行结果的解析,最终返回一个bitmap。
三、GetSimplePhotoActivity
GetSimplePhotoActivity是真正的处理类,用来启动系统相册或者是系统相机来获取图片。在启动系统相册的时候需要有个版本判断,在高版本上获得的uri可能会有点问题,所以这里需要判断不同的版本,进行不同的处理。同时,它还应该对于获得的图片进行回调,返回一个uri。
package com.kale.lib.photo; import com.kale.lib.utils.IntentUtil; 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 java.io.File; /** * @author Jack Tony * @brief * @date 2015/4/24 */ public class GetSimplePhotoActivity extends Activity { private class RequestCode { public static final int ALBUM_OK = 1, ALBUM_OK_KITKAT = 2, CAMERA_OK = 3; } /** * 准备通过什么样的方式来获取图片 */ public static final String KEY_FROM_WAY = "key_from_way"; /** * 图片的全部路径 */ public static final String KEY_PHOTO_PATH = "key_photo_path"; /** * 裁剪好的图片uri */ public static final String KEY_CHOICE_PHOTO_OK_URI = "key_crop_photo_uri"; public static final int VALUE_FROM_ALBUM = 54345; public static final int VALUE_FROM_CAMERA = 46632; public static final String TEMP_PHOTO_FILE_NAME = "kale_temp_photo.jpg"; private File tempPicFile; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (!IntentUtil.isBundleEmpty(getIntent())) { Bundle bundle = getIntent().getExtras(); if (bundle.getInt(KEY_FROM_WAY, VALUE_FROM_ALBUM) == VALUE_FROM_ALBUM) { // 进行版本判断 see:http://blog.csdn.net/tempersitu/article/details/20557383 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { GetSimplePhotoUtil.choicePicFromAlbum_kitkat(this, RequestCode.ALBUM_OK_KITKAT); } else { // 4.4以下 GetSimplePhotoUtil.choicePicFromAlbum(this, RequestCode.ALBUM_OK); } } else { if (bundle.getString(KEY_PHOTO_PATH) == null) { // 照相得到的图片默认的保存路径,用完后会自动删除 tempPicFile = new File(Environment.getExternalStorageDirectory(), TEMP_PHOTO_FILE_NAME); } else { // 自定义照相得到的图片的保存路径,不会自动删除 tempPicFile = new File(bundle.getString(KEY_PHOTO_PATH)); } tempPicFile.delete();// 清空之前的文件 GetSimplePhotoUtil.choicePicFromCamera(this, tempPicFile, RequestCode.CAMERA_OK); } } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Uri uri; switch (requestCode) { // 如果是直接从相册获取 case RequestCode.ALBUM_OK: //从相册中获取到图片 if (data != null) { uri = data.getData(); finishAndReturnBitmap(data, uri); } else { finish(); } break; case RequestCode.ALBUM_OK_KITKAT: if (data != null) { uri = Uri.parse(GetSimplePhotoUtil.getPath(this, data.getData())); finishAndReturnBitmap(data, uri); } else { finish(); } break; // 如果是调用相机拍照时 case RequestCode.CAMERA_OK: // 当拍照到照片时操作 if (tempPicFile.exists()) { uri = Uri.parse(tempPicFile.getAbsolutePath()); finishAndReturnBitmap(new Intent(), uri); } else { finish(); } break; default: break; } } public Bitmap finishAndReturnBitmap(Intent data, Uri uri) { //Logger.t(TAG).d("uri =" + uri); //设置返回数据 data.putExtra(KEY_CHOICE_PHOTO_OK_URI, uri); setResult(GetSimplePhotoHelper.Get_PHOTO_RESULT_OK, data);//设置给之前启动它的activity的一个返回码 finish(); return null; } }
四、工具类和模型类
GetSimplePhotoUtil
package com.kale.lib.photo; import android.annotation.SuppressLint; import android.app.Activity; import android.content.ContentUris; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.media.ExifInterface; import android.net.Uri; import android.os.Build; import android.os.Environment; import android.provider.DocumentsContract; import android.provider.MediaStore; import java.io.File; import java.io.IOException; /** * @author Jack Tony * @brief * @date 2015/4/25 */ class GetSimplePhotoUtil { /** * 从相册获取图片 */ public static void choicePicFromAlbum(Activity activity, int requestCode) { // 来自相册 Intent albumIntent = new Intent(Intent.ACTION_GET_CONTENT); albumIntent.addCategory(Intent.CATEGORY_OPENABLE); albumIntent.setType("image/*"); activity.startActivityForResult(albumIntent, requestCode); } /** * 4.4以上版本使用 * @see "http://blog.csdn.net/tempersitu/article/details/20557383" * * @param activity * @param requestCode */ public static void choicePicFromAlbum_kitkat(Activity activity, int requestCode) { // 来自相册 Intent albumIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT); albumIntent.addCategory(Intent.CATEGORY_OPENABLE); albumIntent.setType("image/*"); activity.startActivityForResult(albumIntent, requestCode); } /** * 拍照后获取图片 * * @param activity * @param cameraPhotoFile 照片的文件 * @param requestCode */ public static void choicePicFromCamera(Activity activity, File cameraPhotoFile,int requestCode) { // 来自相机 Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 下面这句指定调用相机拍照后的照片存储的路径,这样通过这个uri就可以得到这个照片了 cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraPhotoFile)); activity.startActivityForResult(cameraIntent, requestCode);// CAMERA_OK是用作判断返回结果的标识 } private int aspectX = 1000; private int aspectY = 1000; private int outputX = 1; private int outputY = 1; private boolean shouldBeClip = false; private boolean shouldBeScale = false; /** * 设置比率,比如1:1 */ public void setAspectX(int aspectX, int aspectY) { this.aspectX = aspectX; this.aspectY = aspectY; } /** * 设置裁剪出来的图片的宽高 */ public void setOutputX(int outputX, int outputY) { this.outputX = outputX; this.outputY = outputY; } /** * 裁剪时是否保留图片的比例,如果是true那么就是保留 */ public void setShouldBeScale(boolean shoubeScale) { this.shouldBeScale = shoubeScale; } /** * 4.4得到的uri,需要以下方法来获取文件的路径 * * @param context * @param uri * @return */ @SuppressLint("NewApi") public static String getPath(final Context context, final Uri uri) { final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; // DocumentProvider if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { // ExternalStorageProvider if (isExternalStorageDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; if ("primary".equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[1]; } // TODO handle non-primary volumes } // DownloadsProvider else if (isDownloadsDocument(uri)) { final String id = DocumentsContract.getDocumentId(uri); final Uri contentUri = ContentUris.withAppendedId( Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); return getDataColumn(context, contentUri, null, null); } // MediaProvider else if (isMediaDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } final String selection = "_id=?"; final String[] selectionArgs = new String[] { split[1] }; return getDataColumn(context, contentUri, selection, selectionArgs); } } // MediaStore (and general) else if ("content".equalsIgnoreCase(uri.getScheme())) { // Return the remote address if (isGooglePhotosUri(uri)) return uri.getLastPathSegment(); return getDataColumn(context, uri, null, null); } // File else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } return null; } /** * Get the value of the data column for this Uri. This is useful for * MediaStore Uris, and other file-based ContentProviders. * * @param context The context. * @param uri The Uri to query. * @param selection (Optional) Filter used in the query. * @param selectionArgs (Optional) Selection arguments used in the query. * @return The value of the _data column, which is typically a file path. */ private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; final String[] projection = { column }; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int index = cursor.getColumnIndexOrThrow(column); return cursor.getString(index); } } finally { if (cursor != null) cursor.close(); } return null; } /** * @param uri The Uri to check. * @return Whether the Uri authority is ExternalStorageProvider. */ private static boolean isExternalStorageDocument(Uri uri) { return "com.android.externalstorage.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is DownloadsProvider. */ private static boolean isDownloadsDocument(Uri uri) { return "com.android.providers.downloads.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is MediaProvider. */ private static boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is Google Photos. */ private static boolean isGooglePhotosUri(Uri uri) { return "com.google.android.apps.photos.content".equals(uri.getAuthority()); } /** * 得到图片的方向 * * @param photoUri * @return */ public static int getOrientation(final Uri photoUri) { ExifInterface exifInterface = null; try { exifInterface = new ExifInterface(photoUri.toString()); } catch (IOException e) { e.printStackTrace(); } return exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1); } /** * 通过photo的uri来得到图片的角度,从而判断是否需要进行旋转操作 * * @param uri * @return */ public static int getPhotoDegreeByUri(Uri uri) { int degree = 0; int orientation = GetSimplePhotoUtil.getOrientation(uri); if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { degree = 90; } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { degree = 180; } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { degree = 270; } return degree; } }
SimplePhoto
package com.kale.lib.photo; import android.graphics.Bitmap; import android.net.Uri; /** * @author Jack Tony * @brief * @date 2015/4/26 */ public class SimplePhoto { /** * 图片的uri,其实就是地址。eg:/storage/sdcard0/Tencent/QQ_Images/-1935240a504f548c.jpg */ public Uri uri; /** * 图像需要旋转的角度。方向不正确的图像可以根据这个进行旋转操作 */ public int degree; /** * 图像的bitmap */ public Bitmap bitmap; }
五、配置文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.kale.lib"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <application android:name=".KaleApplication" android:allowBackup="true" android:label="@string/app_name"> <activity android:name=".photo.GetSimplePhotoActivity" android:theme="@android:style/Theme.Translucent" /> </application> </manifest>
最后你可以用这样的形式进行应用啦:
package com.kale.duitanglib.photo;/** * @author Jack Tony * @brief * @date 2015/4/26 */ public class GetSimplePhotoHelperActivity extends KaleBaseActivity { GetSimplePhotoHelper mPhotoHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 去选择图片 mPhotoHelper = new GetSimplePhotoHelper(this); mPhotoHelper.choicePhoto(GetSimplePhotoHelper.FROM_WAY.FROM_ALBUM, null); //mPhotoHelper.choicePhoto(GetSimplePhotoHelper.FROM_WAY.FROM_CAMERA, null); //mPhotoHelper.choicePhoto(GetSimplePhotoHelper.FROM_WAY.FROM_CAMERA, Environment.getExternalStorageDirectory()+ "/my_temp.jpg"); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // 通过回调得到图片 SimplePhoto photo = mPhotoHelper.getSelectedPhoto(resultCode, data); Uri uri = photo.uri; Logger.d(uri.toString()); Logger.d(photo.degree + ""); ImageView view = getView(R.id.imageView); view.setImageBitmap(photo.bitmap); } }
标签:
原文地址:http://www.cnblogs.com/tianzhijiexian/p/4460811.html