在首选项屏幕中获取图像 URI!
所以,我实际上正在寻找的是这个解决方案: 以编程方式从 Android 的内置图库应用程序获取/选取图像,但在首选项中而不是在 Activity 中工作。
我创建了一个放在设置中的类,我希望使用该类能够找到用户在手机上选择的图像的 URI。由于“startActivityForResult”,我无法使用该链接中的代码。这在首选项中不可用。
知道如何绕过这个吗?
进度报告:
所以,我同时尝试了一些东西。相反,在我的设置中,我添加了以下内容:
<Preference
android:key="test"
android:title="open image"
android:persistent="true">
<intent android:action="android.intent.action.GET_CONTENT"
android:value="image/*"/>
</Preference>
首先,它不是“GET_CONTENT”,而是“PICK”,但有了它,我只能使用Google Docs和Sim Card Tool Kit,这与图库或任何浏览文件系统的东西。
使用“GET_CONTENT”时,它会崩溃。
我还添加了“android.permission.WRITE_EXTERNAL_STORAGE”权限,即使我只是想阅读。但我没有找到任何问题(如果有办法只要求阅读而不是写作,请将其作为子问题)。
进度报告#2
我用 android:mimeType 替换了 android:value,然后直接进入画廊。现在,只需知道我是否真的获得了 URI 并且问题已解决。
进度报告#3
此时,真正的问题来了。当我浏览 SharedPreferences 时,即使选择图像后,该值仍为空。所以,我想需要做一些小技巧。所以现在,这就是问题。根据上面我的首选项屏幕的示例,假设当调用意图时,它把它放在某个地方,我如何检索“test”的值?
So, what I'm actually looking for is this solution: Get/pick an image from Android's built-in Gallery app programmatically but working in a preference and not in an activity.
I created a class that I put in my setting and I want to use that class to be able to find the URI of an image selected by the user on its phone. I couldn't use the code in that link because of "startActivityForResult". Which is not available in a preference.
Any idea how to bypass this ?
PROGRESS REPORT:
So, I tried some stuff in the mean time. Instead, in my settings, I added the following:
<Preference
android:key="test"
android:title="open image"
android:persistent="true">
<intent android:action="android.intent.action.GET_CONTENT"
android:value="image/*"/>
</Preference>
First, instead of "GET_CONTENT", it was "PICK", but with it, I can only go with Google Docs and the Sim Card Tool Kit, which is far from the gallery or any thing to browse the file system.
With "GET_CONTENT", it crashes.
I also added the "android.permission.WRITE_EXTERNAL_STORAGE" premission, even if I just want to read. But I didn't found any (let put this as a sub question, if there's a way to just ask to read and not write).
PROGRESS REPORT #2
I replaced android:value by android:mimeType and I goes directly to the gallery. Now, just need to know if I really got the URI and it's solved.
PROGRESS REPORT #3
At this point, here's the real problem. When I go through my SharedPreferences, the value stays empty, even after selecting an images. So, I guess there a little hack to do. So now, that's the question. Based on the example of my preference screen above, how can I retrieve the value of "test", assuming that when the intent is called, it put it somewhere ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

我终于找到了一个解决方案......它不像我想要的那样风水,但它有效。
我只需手动编写首选项屏幕,然后就可以访问“startActivityForResult”函数。
至此,花了3到5分钟的时间解决并敲定所有细节。
希望它能帮助一些人。
你有一个例子: http://www.anddev.org/code-snippets-for-android-f33/preference-screen-with-custom-views-t8472.html
但是我使用的那个是谷歌,但我找不到链接。
I finally found a solution... it's not as feng shui as I would like, but it works.
I simply code the preference screen by hand and then I was able to access the "startActivityForResult" function.
From this point, it took 3 to 5 minutes to solve it and finalize all the details.
Hope it helps some people.
You have an example there: http://www.anddev.org/code-snippets-for-android-f33/preference-screen-with-custom-views-t8472.html
But the one I used was on google, but I couldn't find the link.