从画廊中挑选一张图片[英] Pick an image from the Gallery

本文是小编为大家收集整理的关于从画廊中挑选一张图片的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我看过很多关于这个的帖子,看起来下面的代码应该可以工作.我已经创建了一个 SD 卡映像并将其添加到模拟器(并且工作正常).

        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        //intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, 1);

它确实启动并允许选择图像,但是当我单击图像时,一切都会退出并且模拟器返回到主屏幕,而不是返回到我的应用程序.我的 onActivityResult 也从未被调用.

我错过了什么?

推荐答案

发现了我的问题.我正在从一个子活动启动图库,而该子活动 Intent 具有标志 FLAG_ACTIVITY_NO_HISTORY,这阻止了回调返回到该活动.

谢谢.

其他推荐答案

使用以下意图:

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
        intent.setType("image/*");
        intent.putExtra("return-data", true);
        startActivityForResult(intent, 1);

本文地址:https://www.itbaoku.cn/post/102496.html

问题描述

I have seen a lot of posts about this, and it seems like the code below should work. I have created an SD Card image and added it to the emulator (and that works fine).

        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        //intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, 1);

It does launch and allow selection of images, but when I click on an image, everything exits and the emulator returns to the home screen, not the back to my app. My onActivityResult is never called either.

What am I missing?

推荐答案

I found my issue. I was launching the gallery from a sub-activity and that sub activity Intent had the flag FLAG_ACTIVITY_NO_HISTORY which prevented the call back from going to that activity.

thanks.

其他推荐答案

Use the following intent :

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
        intent.setType("image/*");
        intent.putExtra("return-data", true);
        startActivityForResult(intent, 1);