赞
踩
可选择使用相机拍摄和从图库中取图片,并进行裁剪缩放。区分4.3和4.4,主要是因为4.4得到的uri跟4.3是有差别的。从图库选择图片可使用Intent.ACTION_PICK,和Intent.ACTION_GET_CONTENT,4.4以上建议使用的是Intent.ACTION_OPEN_DOCUMENT。
这三种有什么差别呢:
Intent.ACTION_PICK:
4.3得到的uri和4.4得到的uri是一样的 content://media/external/images/media/133
Intent.ACTION_GET_CONTENT:
4.3得到的是content://media/external/images/media/14
4.4得到的是content://com.android.providers.media.documents/document/image%3A133
Intent.ACTION_OPEN_DOCUMENT:
4.3不能使用,会报错android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.OPEN_DOCUMENT typ=image/* }
4.4结果content://com.android.providers.media.documents/document/image%3A133
得到的uri格式是这种类型的话content://media/external/images/media/14 我们很容易就可以得到图片的路径,但是如果图片路径是content://com.android.providers.media.documents/document/image%3A133这种类型的话就需要去解析这个uri了,网上有很多解析content的代码,我使用的是下面这种
package com.example.imageselector;
import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
@SuppressLint("NewApi")
public class PathUtils {
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.
*/
public 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.
*/
public 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.
*/
public 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.
*/
public 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.
*/
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
}
根据传进去的uri解析得到完整的路径 /storage/emulated/0/XXX
用户选择完图片后可以进行裁剪来选择图片,代码如下
public void startPhotoZoom(Uri uri, Uri cropOutPutUri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 100);
intent.putExtra("outputY", 100);
//return-data =false 意味着裁剪成功后不能在onActivityResult 的intent 中获得图片
intent.putExtra("return-data", false);
//裁剪后的图片输出至 cropOutPutUri
intent.putExtra(MediaStore.EXTRA_OUTPUT, cropOutPutUri);
intent.putExtra("scale", true);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
activity.startActivityForResult(intent, REQUEST_CODE_CROP);
}
我选择的是裁剪完成后输出到指定的uri地址,所以我设置return-data为false,如果return-data设为true的话,在onActivityResult中可根据Intent.getExtras().getParcelable(“data”)得到裁剪后的图片。
我之前有用过Intent.getExtras().getParcelable(“data”)在5.1还是在特例手机上有问题,就是得到的是空值,Intent拿不到图片。所以我就使用MediaStore.EXTRA_OUTPUT的方法。
文笔不好写的乱乱的,凑合看吧。不懂看源码最实在
源码直通车 http://download.csdn.net/detail/yxdspirit/9221265
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。