Android:图库意图返回 resultCode == RESULT_CANCELED
我开始打算从图库中选择一张图片,但该意图总是返回结果代码 RESULT_CANCELED。我尝试了很多不同的代码,但没有任何帮助,这让我觉得我可能错过了一些东西,比如在 Android 清单的活动中添加一些东西?
我的代码:
// The Intent
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
Uri targetUri = data.getData();
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
profileImage.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
I'm starting an intent to pick a picture from the gallery but the intent always returns with the resultcode RESULT_CANCELED. I have tried a lot of different code but nothing helps which makes me think maybe I am missing something, like putting something in the activity in the Android manifest?
My Code:
// The Intent
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
Uri targetUri = data.getData();
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
profileImage.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,所以我解决了这个问题。我的问题是在 Gallery Intent 完成之前调用了 onActivityResult() 方法。我在这里找到了解决方案: onActivityResult() 提前调用
基本上,我已将活动指定为“singleTask” “在清单中。
将其更改为“singleTop”为我解决了这个问题。
OK so I solved this. My problem turned out to be that the onActivityResult() method was being called before the Gallery Intent had finished. I found the sollution here: onActivityResult() called prematurely
Basically, I had specified the activity to be "singleTask" in the manifest.
Changing it to "singleTop" solved it for me.
这救了我的命! \0/
android:launchMode="singleTop"
That saved my life! \0/
android:launchMode="singleTop"