直接上代碼:
這些代碼在Activity的onActivityResult方法中
Uri originalUri = data.getData();
String path = "";
int sdkVersion = Integer.valueOf(android.os.Build.VERSION.SDK);
if (sdkVersion > 19) {
path = originalUri.getPath();//5.0直接返回的是圖片路徑,5.0以下是一個和數據庫有關的索引值
} else {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(originalUri,
proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
path = cursor.getString(column_index);
}
picture = BitmapFactory.decodeFile(path, opts);
這樣就可以保證各版本獲取相冊,圖庫等不會報空指針異常了
一直也沒有找到通用的方法,如果誰知道的可能回覆交流下!