2011-07-18 52 views
0

我想顯示圖片庫。問題在於加載它們。[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等級8),並與三星Galaxy S2(安卓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手機圖像的好路子?是他們這是能夠返回專輯的路徑對象?

謝謝:)

(對不起,錯誤)

+0

在我從標準的攝像頭應用程序HTC Ddesire照片保存在:'/ SD卡/ DCIM/100MEDIA /'從'/ SD卡/ DCIM /相機/'的Camera360應用中的照片。 – jamapag

回答

1

我做了一些檢查,它看起來像你試圖訪問MediaStore不正確。它應該通過遊標訪問,而不是直接作爲文件訪問。

這是因爲如在http://developer.android.com/guide/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問題,有一些示例顯示如何使用遊標。

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

最後,基於上面的鏈接,我只想用mediastore對象作爲所有設備上的光標,將解決你與不同的目錄中所遇到的問題。

希望這有助於 喬治

+0

好的喬治。我只是將光標添加到我的代碼,它在仿真器上工作。我要在朋友的手機上測試它(HTC Desire)。當我測試它時,我會向您發送反饋;) – user849944

+0

良好的交易。很高興爲您服務!請告訴我。 –

+0

它的作品!我們可以訪問與HTC的專輯。 Thx爲你提供幫助:) – user849944