Android 列出所有可用图像

发布于 2024-09-05 09:19:43 字数 367 浏览 3 评论 0原文

我正在制作一个应用程序,要求我列出手机 SD 卡上的所有可用图像。

我尝试查询 ContentResolver 方式,

Cursor image = getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI, new String[]{Images.Media._ID,Images.Media.DATA,Images.Media.DISPLAY_NAME}, null, null, null);

但没有任何结果。

有什么方法可以获取列表,或者如果不可能,那么是否有任何可能的意图(例如PICK),我可以通过它允许用户选择一个文件,然后访问用户选择的文件的路径?

I am making an application which requires me to list all the images available on the SD-Card of the phone.

I tried querying the ContentResolver way i.e.

Cursor image = getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI, new String[]{Images.Media._ID,Images.Media.DATA,Images.Media.DISPLAY_NAME}, null, null, null);

but without any result.

Is there any way i can get the list or if that's not possible then is there any possible intent (e.g. PICK) by which I can allow the user to select a file and then access the path of the file the user selected??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

成熟稳重的好男人 2024-09-12 09:19:43
//where contextObject is your activity
ContentResolver cr = contextObject.getContentResolver();

String[] columns = new String[] {
                ImageColumns._ID,
                ImageColumns.TITLE,
                ImageColumns.DATA,
                ImageColumns.MIME_TYPE,
                ImageColumns.SIZE };
cur = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                columns, null, null, null);

我的代码与你的非常相似(除了损坏之外)并且它可以工作。你不需要向画廊意图询问东西,它应该可以工作。我的两个猜测是:

1) 确保您的 USB 存储设备未安装,如果安装了,您将看不到外部图像。

2)也许是权限问题?尝试添加 GLOBAL_SEARCH 权限,看看是否有帮助。

//where contextObject is your activity
ContentResolver cr = contextObject.getContentResolver();

String[] columns = new String[] {
                ImageColumns._ID,
                ImageColumns.TITLE,
                ImageColumns.DATA,
                ImageColumns.MIME_TYPE,
                ImageColumns.SIZE };
cur = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                columns, null, null, null);

My code is pretty much like yours (except broken down) and it works. You don't need to go about asking the Gallery Intent for stuff, it should work. My two guesses are:

1) Make sure your USB storage is not mounted, if it is you'll see no external images.

2) Maybe a permissions issue? Try adding GLOBAL_SEARCH permission to see if that helps at all.

凉世弥音 2024-09-12 09:19:43

您可以使用图库活动来选择图像,如下所示:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_IMAGE);

在活动的回调中,文件 uri 将位于意图参数中

You could use the gallery activity to select images, something like this:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_IMAGE);

in the callback for the activity the file uri will be in the intent parameter

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