[Android]HTC Desire 的图库加载问题:路径错误?

发布于 2024-11-24 22:39:57 字数 1049 浏览 1 评论 0原文

我想展示一个图片库。问题出在加载它们的过程中。

一开始,我使用以下命令加载图像:

images = new File("/sdcard/DCIM/Camera/");
if(images.listFiles().length==0)
   //no images, do some other stuff...
else
   //put images in the gallery and do some stuff

此代码适用于模拟器(Android 2.2 - API Level 8)和三星 Galaxy S2(Android 2.2.1)。但我请一些朋友用自己的手机(HTC、LG...)测试这段代码。

对于 HTC Desire,加载过程中会出现问题。代码将进入“//没有图像”,但他们在相册中有图像(例如来自相机......)我的朋友对我说:

相册存储在内部存储器中,而不是 HTC 的 SD 卡中。

所以我尝试检查其他目录,如下所示:

//Get the internal content
images = new File(MediaStore.Images.Media.INTERNAL_CONTENT_URI.getPath());

if (!images.isDirectory()) {
    //Get the external content
    images = new File(MediaStore.Images.Media.EXTERNAL_CONTENT_URI.getPath());
}

if (!images.isDirectory()) {
    //Get the SD Card content
    images = new File("/sdcard/DCIM/Camera/");
}

但仍然无法正常工作:( 这不是一个好的目录:/

所以,我的问题是:我如何才能获得所有 Android 手机的图像的良好路径?是他们的哪个对象能够返回专辑的路径?

谢谢:)

(抱歉错误)

I want to show an images gallery. The problem is during the loading of them.

At the beginning, I was loading the images with :

images = new File("/sdcard/DCIM/Camera/");
if(images.listFiles().length==0)
   //no images, do some other stuff...
else
   //put images in the gallery and do some stuff

This code works with the emulator (Android 2.2 - API Level 8) and with a Samsung Galaxy S2 (Android 2.2.1). But I ask some friends to test this code with their own phones (HTC, LG...).

With an HTC Desire, there is a problem during this loading. The code is going into "//no images" but they have images in the album (for example from Camera...) My friend said to me that :

album is stored in the internal storage and not in the SD Card with HTC.

So i've tried to check others directory, like this :

//Get the internal content
images = new File(MediaStore.Images.Media.INTERNAL_CONTENT_URI.getPath());

if (!images.isDirectory()) {
    //Get the external content
    images = new File(MediaStore.Images.Media.EXTERNAL_CONTENT_URI.getPath());
}

if (!images.isDirectory()) {
    //Get the SD Card content
    images = new File("/sdcard/DCIM/Camera/");
}

But still not working :( That is not the good directory :/

So, my question is : how can i get the good path for images with all android phone ? is their an Object which is able to return the path of the album ?

Thanks :)

(sorry for mistakes)

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

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

发布评论

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

评论(1

酒废 2024-12-01 22:39:57

我做了一些检查,看起来您正在尝试错误地访问 MediaStore。它应该通过游标访问,而不是直接作为文件访问。

这是因为正如 http://developer.android.com/ 中提到的指南/topics/providers/content-providers.html

内容提供商将其数据公开为数据库模型上的简单表,其中每一行都是一条记录,每一列都是特定类型和含义的数据。

所以基本上你不能将 URI 作为文件读取。它是一个数据表。

以下是同一页面上有关如何访问它的示例。

import android.provider.Contacts.People;
import android.content.ContentUris;
import android.net.Uri;
import android.database.Cursor;

// Use the ContentUris method to produce the base URI for the contact with _ID == 23.
Uri myPerson = ContentUris.withAppendedId(People.CONTENT_URI, 23);

// Alternatively, use the Uri method to produce the base URI.
// It takes a string rather than an integer.
Uri myPerson = Uri.withAppendedPath(People.CONTENT_URI, "23");

// Then query for this specific record:
Cursor cur = managedQuery(myPerson, null, null, null, null);

另外,这里还有另一个 Stackoverflow 问题,其中有一些示例展示了如何使用光标。

如何查询 Android MediaStore 内容提供程序,避免出现孤立图像?

最后,根据上面的链接,我将仅使用 mediastore 对象作为所有设备上的光标,这将解决您在不同目录中遇到的问题。

希望这有帮助,
乔治

I did some checking and it looks like you are trying to access the MediaStore incorrectly. It should be accessed via a cursor and not directly as a file.

This is because as mentioned at http://developer.android.com/guide/topics/providers/content-providers.html

Content providers expose their data as a simple table on a database model, where each row is a record and each column is data of a particular type and meaning.

So basically you can't read the URI as a file. It is a table of data.

Here's an example from that same page on how to access it.

import android.provider.Contacts.People;
import android.content.ContentUris;
import android.net.Uri;
import android.database.Cursor;

// Use the ContentUris method to produce the base URI for the contact with _ID == 23.
Uri myPerson = ContentUris.withAppendedId(People.CONTENT_URI, 23);

// Alternatively, use the Uri method to produce the base URI.
// It takes a string rather than an integer.
Uri myPerson = Uri.withAppendedPath(People.CONTENT_URI, "23");

// Then query for this specific record:
Cursor cur = managedQuery(myPerson, null, null, null, null);

Also, here's another Stackoverflow question that has some examples that shows how to use the cursor.

How to query Android MediaStore Content Provider, avoiding orphaned images?

Finally, based on the links above I would just use the mediastore object as a cursor on all devices and that will solve the problem you are having with different directories.

Hope this helps,
George

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