coder_For_Life22
coder_For_Life22

Reputation: 26971

How to convert URL to a URI?

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;
                                            }

Upvotes: 9

Views: 33315

Answers (4)

Shijil
Shijil

Reputation: 2246

try this

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

URI uri= url.toURI()

Upvotes: 11

JaydeepW
JaydeepW

Reputation: 3285

Refer to this.

http://developer.android.com/reference/android/net/Uri.html#fromFile(java.io.File)

As far as I understand, you are trying to get the Uri of the urls you fetched from remote location. There is strong chance that the Uri wont exist on the device. Just ensure what you trying to achieve.

Upvotes: 0

nhaarman
nhaarman

Reputation: 100358

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.

Upvotes: 5

Nikola Despotoski
Nikola Despotoski

Reputation: 50538

http://developer.android.com/reference/java/net/URL.html#toURI()

See this method. It might be useful for you

Upvotes: 0

Related Questions