convert Url to Uri

14,002

uri not getting from URL

I think something might be wrong with the your url.

I suggest you to try following to check url:

        URI uri = null;
        URL url = null;

Method 1 :

        // Create a URI from url
        try {
            uri = new URI("http://www.google.com/");
            Log.d("URI created: " + uri);
        }
        catch (URISyntaxException e) {
            Log.e("URI Syntax Error: " + e.getMessage());
        }

Method 2

        // Create a URL
        try {
            url = new URL("http://www.google.com/");
            Log.d("URL created: " + url);
        }
        catch (MalformedURLException e) {
            Log.e("Malformed URL: " + e.getMessage());
        }

        // Convert a URL to a URI
        try {
            uri = url.toURI();
            Log.d("URI from URL: " + uri);
        }
        catch (URISyntaxException e) {
            Log.e("URI Syntax Error: " + e.getMessage());
        }

Note :

I want to point out that setImageViewUri (int viewId, Uri uri) is for content URIs particular to the Android platform, not URIs specifying Internet resources.

You can try following method to fetch bitmap from server :

    private Bitmap getImageBitmap(String url) {
            Bitmap bm = null;
            try {
                URL aURL = new URL(url);
                URLConnection conn = aURL.openConnection();
                conn.connect();
                InputStream is = conn.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                bm = BitmapFactory.decodeStream(bis);
                bis.close();
                is.close();
           } catch (IOException e) {
               Log.e(TAG, "Error getting bitmap", e);
           }
       return bm;
    } 
Share:
14,002
M.ArslanKhan
Author by

M.ArslanKhan

Updated on June 04, 2022

Comments

  • M.ArslanKhan
    M.ArslanKhan almost 2 years

    I am working on Widget. I want to set an image (that is available on the server) as Widget background. Following is not working as uri not getting from URL

    The code:

    Uri myUri = Uri.parse("http://videodet.com/cc-content/uploads/thumbs/g45EMgnPwsciIJdOSMQt.jpg");
    remoteViews.setImageViewUri(R.id.widget_img, myUri);
    
  • M.ArslanKhan
    M.ArslanKhan about 10 years
    setImageViewUri (int viewId, Uri uri) this is not setting the image if i get from the server. I have put it in AsynkTask and onpostExecute i have set remoteViews.setImageViewUri(R.id.widget_img, myUri)
  • Ritesh Gune
    Ritesh Gune about 10 years
    Let me give you an example. You have an image on sdcard of android device, you get the uri to that image using contentresolver, and you pass that uri to setImageViewUri. In this case you will get that image readily. But if you do not have image locally but you have it on server(as is your case), you need to download it first using the asynctask as shown above and then use that bitmap with the view.
  • M.ArslanKhan
    M.ArslanKhan about 10 years
    I have folloed like your Idea and ipdate a post about that with complete code. but still no image shown as background.stackoverflow.com/questions/22448242/…