我正在嘗試使用此代碼來緩存這些圖片...如何將URL轉換爲URI?
但是我在這裏不斷收到語法錯誤?
Uri imageUri = new Uri(aURL);
這裏是我使用的代碼。
URL aURL = new URL(myRemoteImages[position]);
Uri imageUri = new Uri(aURL);
if (new File(new File(myContext.getCacheDir(), "thumbnails"), "" + imageUri.hashCode()).exists())
{
String cachFile = ""+imageUri.hashCode();
FileInputStream fis;
try {
fis = new FileInputStream(cachFile);
Bitmap bm = BitmapFactory.decodeStream(fis);
i.setImageBitmap(bm);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
/* Set the Width/Height of the ImageView. */
if(Build.VERSION.SDK_INT >= 11){
i.setLayoutParams(new Gallery.LayoutParams(450, 300));
}
else{
i.setLayoutParams(new Gallery.LayoutParams(125, 125));
}
} catch (FileNotFoundException e) {
Log.e("DEBUGTAG", "Remtoe Image Exception", e);
/* Image should be scaled as width/height are set. */
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
/* Set the Width/Height of the ImageView. */
if(Build.VERSION.SDK_INT >= 11){
i.setLayoutParams(new Gallery.LayoutParams(450, 300));
return i;
}
i.setLayoutParams(new Gallery.LayoutParams(125, 125));
return i;
}
所有URL都是URI,但是並不是所有的URI是URL。 URI可以是相對路徑。只要URL有效(你有一個java.net.URL對象),你可以像下面建議的那樣做。 –
謝謝是的,我通過做imageUri = aUrl.toUri();和沃拉 –
順便說一句..你可以投我的問題了。 –