最近用到上传图片发现,当选择相册里的照片时,会出现无效的URI,android4.4以上系统,选择相册中的最近照片,也会报同样的错误。
找到如下两个方法:
方法一:
/** * 图片选择及拍照结果 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != RESULT_OK) { return; } Uri uri = null; switch (requestCode) { case PHOTOZOOM:// 相册 if (data == null) { return; } uri = data.getData(); String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = managedQuery(uri, proj, null, null, null); int column_index = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); path = cursor.getString(column_index);// 图片在的路径 Intent intent3 = new Intent(mContext, UserRectClipHead.class); intent3.putExtra("path", path); Log.i("选择图片路径",path+""); startActivityForResult(intent3, IMAGE_COMPLETE); break; case PHOTOTAKE:// 拍照 path = photoSavePath + photoSaveName; uri = Uri.fromFile(new File(path)); Intent intent2 = new Intent(mContext, UserRectClipHead.class); intent2.putExtra("path", path); startActivityForResult(intent2, IMAGE_COMPLETE); break; case IMAGE_COMPLETE: final String temppath = data.getStringExtra("path"); Intent intent = new Intent(); saveAllPath = FileUtils.saveBitmap(compressImage(BitmapUtils.UrlToBitmap(temppath)), ((int) (Math.random() * 10000)) + ".png");//质量压缩并存储得到path intent.putExtra("path", saveAllPath); // intent.putExtra("path", temppath); setResult(0, intent); try { cbuilder = new CustomDialogLoading.Builder(mContext, "上传中..."); mDialog = cbuilder.create(); mDialog.show(); postFile(saveAllPath); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } add_img.setImageBitmap(BitmapUtils.UrlToBitmap(temppath)); break; default: break; } super.onActivityResult(requestCode, resultCode, data); }
发现有些手机获取不到图片的path,后来经查找发现是因为android4.4以上的手机相册中有个最近的照片,是获取不到path路径的,更改如下可以解决:
/** * 图片选择及拍照结果 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != RESULT_OK) { return; } Uri uri = null; switch (requestCode) { case PHOTOZOOM:// 相册 if (data == null) { return; } uri = data.getData(); //此处为了解决android4.4 相册选择时的 选择最近图片 获取不到path问题 if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT){//4.4及以上 String wholeID = DocumentsContract.getDocumentId(uri); String id = wholeID.split(":")[1]; String[] column = { MediaStore.Images.Media.DATA }; String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[] { id }, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { path = cursor.getString(columnIndex); } cursor.close(); }else{//4.4以下,即4.4以上获取路径的方法 String[] projection = { MediaStore.Images.Media.DATA }; Cursor cursor = this.getContentResolver().query(uri, projection, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); path = cursor.getString(column_index); } // } Intent intent3 = new Intent(mContext, UserRectClipHead.class); intent3.putExtra("path", path); Log.i("x选择图片",path+""); startActivityForResult(intent3, IMAGE_COMPLETE); break; case PHOTOTAKE:// 拍照 path = photoSavePath + photoSaveName; uri = Uri.fromFile(new File(path)); Intent intent2 = new Intent(mContext, UserRectClipHead.class); intent2.putExtra("path", path); startActivityForResult(intent2, IMAGE_COMPLETE); break; case IMAGE_COMPLETE: final String temppath = data.getStringExtra("path"); Intent intent = new Intent(); saveAllPath = FileUtils.saveBitmap(compressImage(BitmapUtils.UrlToBitmap(temppath)), ((int) (Math.random() * 10000)) + ".png");//质量压缩并存储得到path intent.putExtra("path", saveAllPath); // intent.putExtra("path", temppath); setResult(0, intent); try { cbuilder = new CustomDialogLoading.Builder(mContext, "上传中..."); mDialog = cbuilder.create(); mDialog.show(); postFile(saveAllPath); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } add_img.setImageBitmap(BitmapUtils.UrlToBitmap(temppath)); break; default: break; } super.onActivityResult(requestCode, resultCode, data); }
方法二: