通过使用以下代码,我在4.4.4中获取图像路径,但是当我在棒棒糖5.0.1中使用它时,它无效。
String[] proj = {
MediaStore.Images.Thumbnails._ID,
MediaStore.Images.Thumbnails.DATA,
};
CursorLoader cursorLoader = new CursorLoader(
this, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
Cursor imageCursor = cursorLoader.loadInBackground();
答案 0 :(得分:0)
在需要时调用getSupportLoaderManager启动加载程序管理器。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
imageUri = data.getData();
getSupportLoaderManager().initLoader(0, null, this);
} else if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(this, "Action canceled.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Action failed!", Toast.LENGTH_LONG).show();
}
}
}
然后创建一个用于检索图像路径的游标加载器。
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = {
MediaStore.Images.Media.DATA
};
CursorLoader cursorLoader = new CursorLoader(this, imageUri, projection, null, null, null);
return cursorLoader;
}
当光标加载器完成时,它使用检索到的数据来更新UI。
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
if (data != null) {
int columnIndex = data.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
data.moveToFirst();
imagePath = data.getString(columnIndex);
} else {
imagePath = imageUri.getPath();
}
setupImageView();
}