Android Code to create a thread run network connection get bitmap from url



        new Thread(new Runnable() {
            @Override
            public void run() {
                // Do network action in this function

                try {
                    String baseUrl = "http://image.tmdb.org/t/p/w300";
                    String backdropPathPath = null;
                    try {
                        backdropPathPath = movie.getString("poster_path");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    final String fullImgBackDropUrl = baseUrl + backdropPathPath;
                    URL url2 = new URL(fullImgBackDropUrl);
                    HttpURLConnection connection = 
                                 (HttpURLConnection) url2.openConnection();  
                    connection.setDoInput(true);
                    connection.connect();
                    InputStream input = connection.getInputStream();
                    Bitmap myBitmap = BitmapFactory.decodeStream(input);
                    avgColor = calculateAverageColor(myBitmap, 1);

                } catch (IOException e) {
                    // Log exception
                    Log.d("app", e.getMessage());
                }
            }
        }).start();

Comments

Popular posts from this blog