网上的代码有好多都有问题,在activity返回中没有进行判断,例如魅族pro6调用系统相册后返回的路径为content路径,下面将所有的代码贴上
private final int PHOTO_REQUEST_GALLERY = 0;
启动系统相册activity
Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/*"); // 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_GALLERY startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
处理调用系统相册后返回
@Override protected void onActivityResult(int requestCode, int resultCode, final Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PHOTO_REQUEST_GALLERY) { UIUtils.post(new Runnable() { @Override public void run() { if (data != null) { // 得到图片的全路径 final Uri uri = data.getData(); String scheme = uri.getScheme(); String path; if ("content".equals(scheme)){ path = FileUtil.getContentUrlPath(IdeaActivity.this,uri); }else { path = uri.getPath(); } picUrls.add(BitmapUtil.getUpdateImage(path)); setPictures(); } } }); } }
为毛要在handler中写呢,因为处理图片尤其是大图片时,如果不在hander中写,会显示一下桌面
获取到uri时,要判断uri的scheme是否为content,如果为content,需要在系统数据库中查询图片路径
public static String getContentUrlPath(Activity activity,Uri uri){ String path = null; Cursor cursor = activity.getContentResolver().query(uri, null, null, null,null); if (cursor != null && cursor.moveToFirst()) { path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)); } return path; }