添加一个从相册返回的路径
private String getUrl(Uri uri){
String url = ""; if (!TextUtils.isEmpty(uri.getAuthority())) { try { cursor = getContentResolver().query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null); if (null == cursor) { showAnimator("图片没找到",Constants.STYLE_ALERT); return url; } cursor.moveToFirst(); url = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); }catch (Exception e){ e.printStackTrace(); }finally { if (cursor != null) { cursor.close(); } } } else { url = uri.getPath(); } return url; }这个方法得到一个图片的路径,接下来的事就可以根据这个路径做想做的事~比如上传和设置到页面
try { Uri _uri = data.getData(); if(!TextUtils.isEmpty(getUrl(_uri))){ file1 = new File(getUrl(_uri)); if (file1.exists()) { mBitmap1 = BitmapFactory.decodeFile(getUrl(_uri)); } if (null != mBitmap1) { feedback_fk01.setImageBitmap(Bitmap.createScaledBitmap(mBitmap1, 200, 200, false)); } }else { showAnimator("未找到图片",Constants.STYLE_ALERT); } } catch (Exception ce) { }最后附上选择照片的Intent,因为踩过一个坑,如果intent.setAction(Intent.ACTION_GET_CONTENT);话可能在4.4以后的版本机型会获取不到真实的路径,所以改成
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*"); startActivityForResult(intent, Constants.REQUEST_GALLERY);