By using following code I am getting image path in 4.4.4 but, when I am using it in lollipop 5.0.1, it is not working.
通过使用以下代码我在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();
1 个解决方案
#1
0
Start the loader manager by invoking getSupportLoaderManager when it is needed.
通过在需要时调用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();
}
}
}
Then create a cursor loader that is used to retrieve the image path.
然后创建一个用于检索图像路径的游标加载器。
@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;
}
When the cursor loader is finished it uses the retrieved data to update the UI.
当光标加载器完成时,它使用检索到的数据来更新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();
}
#1
0
Start the loader manager by invoking getSupportLoaderManager when it is needed.
通过在需要时调用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();
}
}
}
Then create a cursor loader that is used to retrieve the image path.
然后创建一个用于检索图像路径的游标加载器。
@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;
}
When the cursor loader is finished it uses the retrieved data to update the UI.
当光标加载器完成时,它使用检索到的数据来更新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();
}