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);
有帮助吗?

解决方案

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;
    } 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top