Calculate the size (width and height) of the image requested from the network

Usually when developing Android to display network pictures, we usually encounter the problem that the displayed picture is deformed due to the different screen size of the mobile phone. Therefore, when we display the picture, we obtain the size of the picture, and then calculate the size that the control should display, so that the picture display will not have the problem of stretching. The following is how to get the size of the network picture, I hope it can help you!

 

// Define the path of a web page image

String url = ”http://www.kksdapp.com/data/upload/552730a23a335.jpg“;

 

// Get the image output stream method according to the image address

// Note: When using this method, call this method in the asynchronous class

public static InputStream getInputStream(Context context, String path){

     InputStream inputStream = null;
     HttpURLConnection httpURLConnection = null;
     String bitmapUrl = null;
     bitmapUrl = path;
     try {
           URL url = new URL(bitmapUrl);
           if(!StringUtil.isNull(url.toString())){
                 httpURLConnection = (HttpURLConnection) url.openConnection();
                 // Set the timeout for connecting to the network
                 httpURLConnection.setConnectTimeout(5000);
                 // Open the input stream
                 httpURLConnection.setDoInput(true);
                 // Set the method used for this Http request
                 httpURLConnection.setRequestMethod("GET" );
                 if (200 == httpURLConnection.getResponseCode()){
                         // Get an input stream from the server
                        inputStream = httpURLConnection.getInputStream();
                 }else{
                        Log.v("inputStream", url+"_link failed");
                 }
          }
      } catch (Exception e) {
          Log.w("url ", e.toString());
          e.printStackTrace();
      }
      return inputStream;
 }

 

// The page starts the asynchronous class to call the above method, converts the obtained image output stream into bitmap format, and then obtains the size of the image

 public static Integer[] getIntentPicSize(final Context context, final String url){
       final Integer[] size = new Integer[2];
       new AsyncTask<Void, Void, Bitmap>() {
           @Override
           protected Bitmap doInBackground(Void... params) {
           System.out.println("url : " + url);
           InputStream is = DownloadBitmap.getInputStream(context, url);
           if (is != null) {
               return BitmapFactory.decodeStream(is);
           } else {
               eturn null;
           }
       }
      @Override
      protected void onPostExecute(Bitmap bitmap) {
            if (bitmap == null) {
                bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.member_home_bg);
            }
           size[0] = bitmap.getWidth();
           size[1] = bitmap.getHeight();
           System.out.println("bitmap.getWidth() : " + bitmap.getWidth());
           System.out.println("bitmap.getHeight() : " + bitmap.getHeight());
           super.onPostExecute(bitmap);
       }
    }.execute();
       return size;
 }

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326758390&siteId=291194637