Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert URL to a URI?

Tags:

android

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;
                                            }
like image 838
coder_For_Life22 Avatar asked Jul 28 '11 19:07

coder_For_Life22


2 Answers

try this

http://www.java2s.com/Tutorials/Java/java.net/URL/Java_URL_toURI_.htm

URI uri= url.toURI()
like image 173
Shijil Avatar answered Sep 19 '22 01:09

Shijil


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.

like image 43
nhaarman Avatar answered Sep 21 '22 01:09

nhaarman