2011-07-28 44 views
3

我正在尝试使用此代码来缓存这些图片...如何将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; 
              } 
+0

所有URL都是URI,但是并不是所有的URI是URL。 URI可以是相对路径。只要URL有效(你有一个java.net.URL对象),你可以像下面建议的那样做。 –

+0

谢谢是的,我通过做imageUri = aUrl.toUri();和沃拉 –

+0

顺便说一句..你可以投我的问题了。 –

回答

4

阅读docs.您无法使用URL创建URI。使用类似

URI uri = new URI(aURL.toString()); 

捕捉任何必要的课程例外。

+1

URI!= Uri。那个构造函数不存在。 – Simian

+0

原谅错字.. – nhaarman