安卓论坛

标题: 怎么SDCard上的获取相册照片 [打印本页]

作者: admin    时间: 2016-1-6 20:30
标题: 怎么SDCard上的获取相册照片
  1. private String getRealPathFromURI(Uri contentUri) {
  2.                 Cursor cursor = null;
  3.                 String result = contentUri.toString();
  4.                 String[] proj = {MediaStore.Images.Media.DATA};
  5.                 cursor = managedQuery(contentUri, proj, null, null, null);
  6.                 if(cursor == null) throw new NullPointerException("reader file field");
  7.                 if (cursor != null) {
  8.                         int column_index = cursor
  9.                                         .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  10.                         cursor.moveToFirst();
  11.                         // 最后根据索引值获取图片路径
  12.                         result = cursor.getString(column_index);
  13.                          try  
  14.                     {  
  15.                         //4.0以上的版本会自动关闭 (4.0--14;; 4.0.3--15)  
  16.                         if(Integer.parseInt(Build.VERSION.SDK) < 14)  
  17.                         {  
  18.                             cursor.close();  
  19.                         }  
  20.                     }catch(Exception e)  
  21.                     {  
  22.                         Log.e(TAG, "error:" + e);  
  23.                     }  
  24.                 }
  25.                 return result;
  26.     }
复制代码







欢迎光临 安卓论坛 (http://android.hh85.com/) Powered by Discuz! X3.2