[Android]HTC Desire 的图库加载问题:路径错误?
我想展示一个图片库。问题出在加载它们的过程中。
一开始,我使用以下命令加载图像:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我做了一些检查,看起来您正在尝试错误地访问 MediaStore。它应该通过游标访问,而不是直接作为文件访问。
这是因为正如 http://developer.android.com/ 中提到的指南/topics/providers/content-providers.html
所以基本上你不能将 URI 作为文件读取。它是一个数据表。
以下是同一页面上有关如何访问它的示例。
另外,这里还有另一个 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
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.
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