如何URL转换为URI? [英] How to convert URL to a URI?
本文介绍了如何URL转换为URI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想缓存使用此code这些图片...
但我一直在这里得到一个语法错误?
乌里imageUri =新的URI(aURL);
下面是code IM使用。
URL aURL =新的URL(myRemoteImages [位置]); 乌里imageUri =新的URI(aURL);
如果(新文件(新文件(myContext.getCacheDir(),缩略图),+ imageUri.hash code())。存在())
{
字符串cachFile =+ imageUri.hash code();
FIS的FileInputStream; 尝试{
FIS =新的FileInputStream(cachFile);
位图BM = BitmapFactory.de codeStream(FIS);
i.setImageBitmap(BM);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
/ *设置的ImageView的宽度/高度。 * /
如果(Build.VERSION.SDK_INT> = 11){
i.setLayoutParams(新Gallery.LayoutParams(450,300));
}
其他{
i.setLayoutParams(新Gallery.LayoutParams(125,125));
}
}赶上(FileNotFoundException异常五){ Log.e(DEBUGTAG,Remtoe图像异常,E); / *为宽/高设置图片应缩放。 * /
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
/ *设置的ImageView的宽度/高度。 * /
如果(Build.VERSION.SDK_INT> = 11){
i.setLayoutParams(新Gallery.LayoutParams(450,300)); 返回我;
}
i.setLayoutParams(新Gallery.LayoutParams(125,125));
返回我;
}
解决方案
阅读的文档。你不能与URL创建一个URI。使用类似
URI URI =新的URI(aURL.toString());
捕获任何必要的例外ofcourse。
I am trying to cache these images using this code...
But i keep getting a syntax error here?
Uri imageUri = new Uri(aURL);
Here is the code im using.
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;
}
解决方案
Read the docs. You can't create an URI with an URL. Use something like
URI uri = new URI(aURL.toString());
Catching any necessary exceptions ofcourse.
这篇关于如何URL转换为URI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文