开发者

Help making an app that will display an image that is from a server?

开发者 https://www.devze.com 2023-02-10 14:26 出处:网络
I am making an app that when a button is clicked, it will grab an image from a URL or server and display it.

I am making an app that when a button is clicked, it will grab an image from a URL or server and display it.

EXAMPLE

开发者_开发问答

App1

Button1 = clicked

Then Image from server or URL will be displayed on screen in app.


The above answer seems correct.

Just on the other note, do all this operation in another thread than the UI Thread. And have an ImageView at handy in your layout so that when you have the image in bitmap, you can set that bitmap into the ImageView so that the user can see it.

Since the image can be quite large in size, if downloaded in UI Thread, it'll cause your app to freeze.

Just reformatting the above answer over here:

try { 
   Bitmap bitmap = BitmapFactory.decodeStream(
                 (InputStream)new URL("http://abc.com/image.jpg").getContent()); 
    }catch (MalformedURLException e) { 
        e.printStackTrace(); 
    } catch (IOException e) {
        e.printStackTrace(); 
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号