- private String getRealPathFromURI(Uri contentUri) {
- Cursor cursor = null;
- String result = contentUri.toString();
- String[] proj = {MediaStore.Images.Media.DATA};
- cursor = managedQuery(contentUri, proj, null, null, null);
- if(cursor == null) throw new NullPointerException("reader file field");
- if (cursor != null) {
- int column_index = cursor
- .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
- cursor.moveToFirst();
- // 最后根据索引值获取图片路径
- result = cursor.getString(column_index);
- try
- {
- //4.0以上的版本会自动关闭 (4.0--14;; 4.0.3--15)
- if(Integer.parseInt(Build.VERSION.SDK) < 14)
- {
- cursor.close();
- }
- }catch(Exception e)
- {
- Log.e(TAG, "error:" + e);
- }
- }
- return result;
- }
//path = path.substring(path.indexOf("/sdcard"), path.length()); 目录不要写死 。