Android:图库意图返回 resultCode == RESULT_CANCELED

发布于 2024-10-21 23:31:41 字数 816 浏览 7 评论 0原文

我开始打算从图库中选择一张图片,但该意图总是返回结果代码 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万水千山粽是情ミ 2024-10-28 23:31:41

好的,所以我解决了这个问题。我的问题是在 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.

向日葵 2024-10-28 23:31:41

这救了我的命! \0/

android:launchMode="singleTop"

That saved my life! \0/

android:launchMode="singleTop"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文