【发布时间】:2021-12-06 23:31:12
【问题描述】:
我是一个超级初学者,所以我只是在关注一些项目,我知道 startActivityforResult 已被弃用,所以我使用 ActivityResultLauncher 更改了代码。但我不知道如何修复 CursorLoader 错误。能告诉我怎么解决吗?
原来的代码是这样的。
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_PROFILE_FROM_ALBUM && resultCode == RESULT_OK) {
String[] proj = {MediaStore.Images.Media.DATA};
CursorLoader cursorLoader = new CursorLoader(this, data.getData(), proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
//image path
String photoPath = cursor.getString(column_index);
}
这是当前代码
ActivityResultLauncher<Intent> startActivityResult = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if(result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
String[] proj = {MediaStore.Images.Media.DATA};
CursorLoader cursorLoader = new CursorLoader(this, data.getData(), proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
int column_index =cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String photoPath = cursor.getString(column_index);
}
所以 CursorLoader 中的上下文应该是固定的。它需要 'Context' 但现在是 ActivityResultCallback。
【问题讨论】:
标签: java android android-cursorloader