Java onActivityResult从未被调用
我想从画廊里挑一幅画。我有一个隐含的意图和一个积极的结果,但它从来没有被称为。 我一直在寻找以下可能的解决方案:Java onActivityResult从未被调用,java,android,android-intent,onactivityresult,Java,Android,Android Intent,Onactivityresult,我想从画廊里挑一幅画。我有一个隐含的意图和一个积极的结果,但它从来没有被称为。 我一直在寻找以下可能的解决方案: setResult(RESULT_LOAD_IMG, galleryIntent); 以下是我在AddQuestionActivity中的代码: private static int RESULT_LOAD_IMG = 1; String imgDecodableString; public static Intent galleryIntent; ima
setResult(RESULT_LOAD_IMG, galleryIntent);
以下是我在AddQuestionActivity中的代码:
private static int RESULT_LOAD_IMG = 1;
String imgDecodableString;
public static Intent galleryIntent;
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Create intent to Open Image applications like Gallery, Google Photos
galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
// Set the Image in ImageView after decoding the String
imageView.setImageBitmap(BitmapFactory
.decodeFile(imgDecodableString));
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
谢谢您的建议。如果有,请在ActivityResult上查看您的基本活动 结果\u负载\u IMG=1
如果在ActivityResult上的基本活动中设置了1,则AddQuestionActivityOnActivityResult可能无法继续请检查您的基本活动onactivityResult(如果有) 结果\u负载\u IMG=1 如果在ActivityResult上的基本活动中设置了1,则AddQuestionActivityOnActivityResult将无法继续更新代码:
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
});
}
更新您的代码:
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
});
}
My baseActivity没有onActivityResult,但感谢您的建议。My baseActivity没有onActivityResult,但感谢您的建议。Toast不是调试应用程序的最佳方法。使用日志并将其放入success中。很可能setImageBitmap没有更改图像视图位图,这会让您相信onActivityResult没有被调用。调试应用程序的最佳方法并不是使用setImageBitmap。使用日志并将其放入success中。setImageBitmap可能没有更改图像视图位图,这会让您相信onActivityResult没有被调用