2014-01-16 18 views
29

我必須將drawable加載到ImgeView中。在這個應用程序中,我出於其他原因使用畢加索。 在這種情況下,我需要使用它的uri加載drawable而不是它的id。 要做到這一點,這裏是我的代碼:Picasso從它們的URI加載drawable資源

uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+context.getPackageName()+"/drawable/" + drawableName); 

然後

Picasso.with(context).load(uri).into(imageView); 

我知道肯定繪製的名字是正確的,但畢加索似乎並不喜歡這個URI。

+0

你不能「加載」anyrhing到可繪文件夾中,資源只讀 – pskink

+0

對不起,我的英文不是很好:) –

+0

它不是你的英文,它的夠好,我的意思是你不能添加任何東西到可繪文件夾中 – pskink

回答

8

找到了答案。不幸的是,畢加索不允許通過URI進行可繪製加載。這是一個傳入功能。

+0

這仍然是這樣嗎? –

+4

畢加索現在支持使用URI。請參閱https://square.github.io/picasso/javadoc/com/squareup/picasso/Picasso.html – Fahim

+0

,但您必須使用picasso jar v2.5才能使用該功能 –

62

如果圖像在您的可繪製文件夾中,那麼您可以加載它。

Picasso.with(context).load(R.drawable.drawableName).into(imageView); 

and picasso will load it no need for a Uri。

+0

我需要動態參考drowables –

+7

對於動態參考使用: INT渣油= this.ctx .getResources()。getIdentifier(「:drawable /」+ imageName,null,null); –

6

正如Picasso的文檔中所述。

,他們現在URI支持加載圖片類似如下:

load(android.net.Uri uri) 

,所以你必須做一些這樣的:

Picasso.with(context).load(uri).into(imageView); 

就像你在做什麼了。

Hopethat help。

+0

請查看https:// github。 com/square/picasso/pull/292 –

+0

好的,這個怎麼樣? http://square.github.io/picasso/javadoc/com/squareup/picasso/Picasso.html#load(android.net.Uri) –

+1

該方法適用於除指向內部資源(例如作爲一個drawable)。即:'Uri.parse(「http://website.com/image.jpg」)是畢加索的有效URI。但是Picasso不能用'Uri.parse(「android.resource:// your_drawable」)加載drawable。正如他們所說,這個功能將可用於畢加索2.2+ –

4
  • 這是如果你不想硬代碼,你會 加載圖像...

您可以從您的文件夾繪製懶洋洋地,如果你加載本地圖像文件知道你想要加載的圖像的整數值。

然後,你可以這樣做:

Picasso.with(getContext()).load(imageResourceId) 
.error(R.drawable.ic_launcher) 
.into(imageView); 

ImageView的

是你要顯示的圖像視圖。例如:

imageView = (ImageView) convertView 
.findViewById(R.id.itemImage); 

在哪裏

imageResourceId

是可拉伸的整數值。您可以通過檢索該整型值:

int productImageId = resources.getIdentifier(
productImageName, "drawable", context.getPackageName()); 

productImageName

是你想畫(即 「ic_launcher」)

這個可繪製的名字全部都可以通過ADAPTER

+0

偉大的幫助!它像一個魅力一樣工作! – sud007