選擇圖片后 onActivityResult中的代碼如下:
Uri uri = data.getData();
if (uri != null)
{
mFilePath = new URIUtils().getPathFromUri(uri);
}
解決辦法如注釋中的所示。 4.0以上平臺會自動關閉cursor
protected String getPath(Uri uri)
{
String filePath = "";
String[] projection = {MediaColumns.DATA };
Cursor cursor = managedQuery(uri,
projection,
null,
null,
null);
if (cursor != null)
{
int columnIndex = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
filePath = cursor.getString(columnIndex);
try
{
//4.0以上的版本會自動關閉 (4.0--14;; 4.0.3--15)
if(Integer.parseInt(Build.VERSION.SDK) < 14)
{
cursor.close();
}
}catch(Exception e)
{
Log.error(TAG, "error:" + e);
}
}
return filePath;
}
posted on 2012-08-31 15:16
小果子 閱讀(1075)
評論(1) 編輯 收藏 引用 所屬分類:
Android & Ios