我很難讓Thumbnails.getThumbnail在運行ICS的Nexus S上使用Picasa照片。其他一切似乎都起作用,包括從Picasa獲取原始圖像並顯示它,但getThumbnail似乎不起作用。縮略圖和從ICS上的庫中選取picasa圖像
E/MiniThumbFile(1852): Got exception when reading magic, id = 5745625138093120418, disk full or mount read-only? class java.lang.IllegalArgumentException
W/MediaProvider( 540): original media doesn't exist or it's canceled.
的磁盤未滿,它是可讀寫的應用程序有外部存儲寫入權限,以及圖片並在Picasa上確確實實存在(我:嘗試使用它時,我發現了以下錯誤可以通過Android Gallery應用程序查看它)。
相同的代碼在Android 2.3上工作正常,但它遵循一個稍微不同的路徑,因爲2.3好像下載了一張照片並將實際的本地文件:// uri傳遞給新下載的映像,而不是遞給你一個內容:// uri。
這是有問題的代碼的主要肉類:當過新的照片被添加到應用程序的照片「收藏」
public void addImage(Uri uri, boolean local)
{
ContentResolver resolver = getContentResolver();
Uri actualUri = uri;
Log.d(TAG, "addImage: original uri: " + uri.toString());
if(local) {
try {
List<String> uriPath = uri.getPathSegments();
String contentUri = Media.insertImage(resolver, uri.getPath(), uriPath.get(uriPath.size()-1), new String());
actualUri = Uri.parse(contentUri);
}
catch(java.io.FileNotFoundException ex) {
Log.e(TAG, "FileNotFoundException: ");
ex.printStackTrace();
}
}
Log.d(TAG, "addImage: actual uri: " + actualUri.toString());
List<String> uriPath = actualUri.getPathSegments();
long imageId = Long.parseLong(uriPath.get(uriPath.size() -1));
Bitmap thumb = Thumbnails.getThumbnail(resolver, imageId, Thumbnails.MINI_KIND, null);
if(thumb == null) {
Log.e(TAG, "Failed to get thumbnail for our image.");
Toast toast = Toast.makeText(getApplicationContext(), "Failed to get thumbnail for image. Please try again.", Toast.LENGTH_SHORT);
toast.show();
return;
}
uris.add(uri);
bmps.add(thumb);
notifyDataSetChanged();
}
該方法被調用。當它被稱爲本地圖像(即:如果照片是從應用程序內部拍攝的,或者onActivityResult的數據參數爲null),則將本地參數設置爲true,然後嘗試從內容獲取content:// uri媒體內容提供商,所以我們可以得到一個有效的圖像ID傳遞給Thumbnails.getThumbnail。該代碼適用於從相機應用程序獲取的圖像(通過startActivityForResult)以及庫中本地存儲在設備上的圖像。
我有點難住。
對我來說,最好的答案:http://stackoverflow.com/a/8936697/994324
它的工作對所有的情況下(實際上):Picasa和本地圖片,並且它更乾淨。 – 2012-12-27 22:28:25
@Tomasu ..非常感謝你親愛的我用問題的示例代碼解決了我的問題。無論如何,我想與你分享我的代碼在回答。希望它也能幫助其他人。 :) – swiftBoy