gpt4 book ai didi

android - 使用 CursorLoader 和 LoaderManager 从 Android 应用程序中检索图像

转载 作者:太空狗 更新时间:2023-10-29 15:38:54 25 4
gpt4 key购买 nike

目前我正在使用 getContentResolver().query()/managedQuery() 获取光标以从图库应用程序中检索图像。因为我使用的 API 已部分弃用,所以我想将 CursorLoader 与 LoaderManager 结合使用。

/**
* Creates a cursor to access the content defined by the image uri for API
* version 11 and newer.
*
* @return The created cursor.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private Cursor createCursorHoneycomb() {
String[] projection = {
MediaStore.Images.Media.DATA
};
Cursor cursor = getContentResolver().query(imageUri, projection, null, null, null);

return cursor;
}

/**
* Creates a cursor to access the content defined by the image uri from API
* version 8 to 10.
*
* @return The created cursor.
*/
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.FROYO)
private Cursor createCursorFroyo() {
String[] projection = {
MediaStore.Images.Media.DATA
};
Cursor cursor = managedQuery(imageUri, projection, null, null, null);

return cursor;
}

因为我没有 ListView,所以我不使用任何适配器。我只是为 ImageView 设置了图像位图。

/**
* Sets the image bitmap for the image view.
*/
private void setupImageView() {
String imagePath = getImagePathFromUri(imageUri);
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
ImageView imageView = (ImageView) findViewById(R.id.image_view);

imageView.setImageBitmap(bitmap);
}

/**
* Returns an image path created from the supplied image uri.
*
* @param imageUri The supplied image uri.
* @return Returns the created image path.
*/
@SuppressWarnings("deprecation")
private String getImagePathFromUri(Uri imageUri) {
Cursor cursor = null;
String imagePath = null;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
cursor = createCursorHoneycomb();
} else {
cursor = createCursorFroyo();
}

// if image is loaded from gallery
if (cursor != null) {
startManagingCursor(cursor);

int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

cursor.moveToFirst();
imagePath = cursor.getString(columnIndex);
}
// if image is loaded from file manager
else {
imagePath = imageUri.getPath();
}

return imagePath;
}

是否可以将 CursorLoader 与 LoaderManager 一起使用以从图库应用程序或文件管理器加载图像?我找不到任何解决方案。

最佳答案

在需要时通过调用 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();
}

这很容易做到。但我必须了解如何使用 onCreateLoader() 和 onLoadFinished()。

关于android - 使用 CursorLoader 和 LoaderManager 从 Android 应用程序中检索图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14424624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com