Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8177235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:27:31+00:00 2026-06-06T23:27:31+00:00

Hi guys. I have a problem when downloading large size images.It’s very strange, while

  • 0

Hi guys. I have a problem when downloading large size images.It’s very strange, while read bytes from stream always no response.
My code is as follows, any suggestion is welcome.

public class ImageTestActivity extends Activity {

    public static final int IMAGE_BUFFER_SIZE = 8*1024;
    public static final int MAX_REQUEST_WIDTH = 480;
    public static final int MAX_REQUEST_HEIGHT = 480;
    private static final String TAG = ImageTestActivity.class.getSimpleName();
    private static final int HTTP_CONNECT_TIMEOUT = 10000;

    private static final int CONTENT_IMAGE_OFFSET = 80;
    private Display mDisplay = null;

    private ImageView mContentPic = null;

    private Bitmap mContentPicBitmap = null;

    private RefreshAsyncTask mRefreshTask = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
        mContentPic = (ImageView)findViewById(R.id.wimessage_content_picture);
        mDisplay = getWindowManager().getDefaultDisplay();
        mRefreshTask = new RefreshAsyncTask();
        mRefreshTask.execute("http://218.240.46.38/img/201206/28/-980416187.jpeg");
    }    

    private void initImageSetting(Bitmap bm) {
        if (bm == null) {
            return;
        }
        int scrWidth = mDisplay.getWidth();
        int scrHeight = mDisplay.getHeight();
        int imageHeight = bm.getHeight();
        int imageWidth = bm.getWidth();
        /*if (imageHeight*3 < imageWidth*2) {
             * It is very strange, when the picture aspect ratio less than 3:2, 
             * execute the following code will cause the picture is not displayed
             *
            return;
        }*/

        mContentPic.setAdjustViewBounds(true);
        mContentPic.setMaxWidth(scrWidth - CONTENT_IMAGE_OFFSET);
        if ((imageWidth <= scrWidth - CONTENT_IMAGE_OFFSET) || (imageHeight < scrHeight)) {
            mContentPic.setMaxHeight(imageHeight);
        } else {
            mContentPic.setMaxHeight((int)((float)imageHeight * (scrWidth - CONTENT_IMAGE_OFFSET) / imageWidth));       
        }
    }

    public static byte[] getBytes(BufferedInputStream inStream) throws IOException {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        BufferedOutputStream out = new BufferedOutputStream(outStream, IMAGE_BUFFER_SIZE);
        byte[] buffer = new byte[IMAGE_BUFFER_SIZE];

        int len = inStream.read(buffer);
        Log.i(TAG, "---start---");
        while (len != -1) {
            Log.i(TAG, ((Integer)len).toString());
            try {
                out.write(buffer, 0, len);
            } catch (IndexOutOfBoundsException e) {
                e.printStackTrace();
            }
            len = inStream.read(buffer);
        }

        Log.i(TAG, "---end---");
        out.flush();
        out.close();
        inStream.close();

        return outStream.toByteArray();
    }

    public static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                inSampleSize = Math.round((float) height / (float) reqHeight);
            } else {
                inSampleSize = Math.round((float) width / (float) reqWidth);
            }

            // This offers some additional logic in case the image has a strange
            // aspect ratio. For example, a panorama may have a much larger
            // width than height. In these cases the total pixels might still
            // end up being too large to fit comfortably in memory, so we should
            // be more aggressive with sample down the image (=larger
            // inSampleSize).

            final float totalPixels = width * height;

            // Anything more than 2x the requested pixels we'll sample down
            // further.
            final float totalReqPixelsCap = reqWidth * reqHeight * 2;

            while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
                inSampleSize++;
            }
        }
        return inSampleSize;
    }    

    public static Bitmap loadImageFromURL(String urlPath) {
        try {
            URL url = new URL(urlPath);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(HTTP_CONNECT_TIMEOUT);
            int rspCode = connection.getResponseCode();
            if (rspCode == HttpStatus.SC_OK) {
                //InputStream in = connection.getInputStream();
                Bitmap bitmap = null;
                BufferedInputStream in = new BufferedInputStream(url.openStream(), IMAGE_BUFFER_SIZE);
                byte[] data = getBytes(in);
                in.close();
                if (data != null) {
                    try {
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inJustDecodeBounds = true;
                        options.inSampleSize = calculateInSampleSize(options, MAX_REQUEST_WIDTH, MAX_REQUEST_HEIGHT);
                        options.inJustDecodeBounds = false;
                        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
                    } catch (OutOfMemoryError e) {
                        e.printStackTrace();
                    }
                } else {
                    Log.i(TAG, "data == null");
                }

                connection.disconnect();                
                return bitmap;
            } else {
                connection.disconnect();
                Log.i(TAG, "rspCode = " + rspCode);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    private class RefreshAsyncTask extends AsyncTask<String, Boolean, Boolean> {
        @Override
        protected Boolean doInBackground(String... arg0) {
            mContentPicBitmap = loadImageFromURL(arg0[0]);
            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            if (mContentPicBitmap != null) {
                initImageSetting(mContentPicBitmap);
                mContentPic.setImageBitmap(mContentPicBitmap);
            }
        }
    }    
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:27:33+00:00Added an answer on June 6, 2026 at 11:27 pm

    try this i have try with ur links it show me an image in imageview

    BitmapFactory.Options bmOptions;
    bmOptions = new BitmapFactory.Options();
    bmOptions.inSampleSize = 1;
    bm = LoadImage("http://218.240.46.38/img/201206/28/-980416187.jpeg", bmOptions);
    imageview.setImageBitmap(bm);
    

    where methos LoadImage is as given below

    private Bitmap LoadImage(String URL, BitmapFactory.Options options){      
        Bitmap bitmap = null;
        InputStream in = null;      
        try {
            in = OpenHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in, null, options);
            in.close();
        } catch (IOException e1) {
    
        }
       return bitmap;              
    }
    
    
    private InputStream OpenHttpConnection(String strURL) throws IOException {
         InputStream inputStream = null;
         URL url = new URL(strURL);
         URLConnection conn = url.openConnection();
    
         try{
            HttpURLConnection httpConn = (HttpURLConnection)conn;
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                inputStream = httpConn.getInputStream();
            }
         } catch (Exception ex){
    
         }
         return inputStream;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

guys. I have a strange problem. I try to write unit-tests to web-app. I
Hi guys I have a problem of images showing background-color on IE6 and its
Hi guys I have a very frustrating and strange thing happening here, when I
Hello guys i have a problem while trying to use the 'radio' input in
Hi guys i have a problem i my code i set a datepicker with
Ok guys, I have a serious problem with this. I have a static class
Hey guys, I have a weird problem. I have an update system that refreshes
hey guys having this really simple problem but cant seem to figure out have
I hope some of you guys can help me with this problem.... I have
Hey guys I have a problem with an app I'm making. The thing is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.