版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhanwei0102/article/details/53428682
public interface PhotoCallBack {
void onSuccess(String picturePath);// 拿取相片成功
void onFailure();// 拿取相片失败
}
/**
* 获取图片路径
*
* @param context
* @param data
* @param callback
* @return
*/
public static void getPhotoURLByAlbum(Context context, Intent data, PhotoCallBack callback) {
if (data == null) {
callback.onFailure();
return;
}
final Uri selectedImage = data.getData();
if (selectedImage == null) {
callback.onFailure();
return;
}
String picturePath = ""; // 关于Android4.4的图片路径获取,如果回来的Uri的格式有两种
if (Build.VERSION.SDK_INT >= 19 && DocumentsContract.isDocumentUri(context, selectedImage)) {
String wholeID = DocumentsContract.getDocumentId(selectedImage);
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[] { id }, null);
if (cursor.moveToNext()) {
int columnIndex = cursor.getColumnIndex(column[0]);
picturePath = cursor.getString(columnIndex);
callback.onSuccess(picturePath);// 获取图片路径
}
cursor.close();
} else {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(selectedImage, projection, null, null, null);
if (cursor.moveToNext()) {
int column_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
picturePath = cursor.getString(column_index);
callback.onSuccess(picturePath);// 获取图片路径
}
cursor.close();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
在onActivityResult中处理返回的数据。
同时一定要在打开相册的逻辑中做判断
Intent intent = new Intent();
intent.setType("image/*");// 从所有图片中进行选择
//根据版本号不同使用不同的Action
if (Build.VERSION.SDK_INT <19) {
intent.setAction(Intent.ACTION_GET_CONTENT);
}else {
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
}
((Activity) context).startActivityForResult(intent, Constants.REQUEST_CODE_HEADPIC_PICK_PHOTO);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
参考:
http://blog.csdn.net/huangyanan1989/article/details/17263203
http://blog.csdn.net/eastman520/article/details/17756817
public interface PhotoCallBack {
void onSuccess(String picturePath);// 拿取相片成功
void onFailure();// 拿取相片失败
}
/**
* 获取图片路径
*
* @param context
* @param data
* @param callback
* @return
*/
public static void getPhotoURLByAlbum(Context context, Intent data, PhotoCallBack callback) {
if (data == null) {
callback.onFailure();
return;
}
final Uri selectedImage = data.getData();
if (selectedImage == null) {
callback.onFailure();
return;
}
String picturePath = ""; // 关于Android4.4的图片路径获取,如果回来的Uri的格式有两种
if (Build.VERSION.SDK_INT >= 19 && DocumentsContract.isDocumentUri(context, selectedImage)) {
String wholeID = DocumentsContract.getDocumentId(selectedImage);
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[] { id }, null);
if (cursor.moveToNext()) {
int columnIndex = cursor.getColumnIndex(column[0]);
picturePath = cursor.getString(columnIndex);
callback.onSuccess(picturePath);// 获取图片路径
}
cursor.close();
} else {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(selectedImage, projection, null, null, null);
if (cursor.moveToNext()) {
int column_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
picturePath = cursor.getString(column_index);
callback.onSuccess(picturePath);// 获取图片路径
}
cursor.close();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
在onActivityResult中处理返回的数据。
同时一定要在打开相册的逻辑中做判断
Intent intent = new Intent();
intent.setType("image/*");// 从所有图片中进行选择
//根据版本号不同使用不同的Action
if (Build.VERSION.SDK_INT <19) {
intent.setAction(Intent.ACTION_GET_CONTENT);
}else {
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
}
((Activity) context).startActivityForResult(intent, Constants.REQUEST_CODE_HEADPIC_PICK_PHOTO);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
参考:
http://blog.csdn.net/huangyanan1989/article/details/17263203
http://blog.csdn.net/eastman520/article/details/17756817