简体   繁体   中英

Bitmap image compress from url

Hello i have facebook image that i have to compress and put it in my imageview. I used below code to resize my image and compress it so that i can show it in my imageview but it gives file not found exception error

i do not find any way to compress file/image that located on server. you can take bitmap from URL and the you suppose to re size.

For Getting Bitmap From URL.

URL url = new URL("http://....");
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());

for resize you can use below code

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);

// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}

How to use :- place this code:-

private void showImage(final String URL) {
    new Thread(new Runnable() {

        @Override
        public void run() {
            URL url = new URL(URL);
            Bitmap bm = BitmapFactory.decodeStream(url.openConnection()
                    .getInputStream());

            int width = bm.getWidth();
            int height = bm.getHeight();
            float scaleWidth = ((float) YOUR_WIDTH) / width;
            float scaleHeight = ((float) YOUR_HEIGHT) / height;
            // CREATE A MATRIX FOR THE MANIPULATION
            Matrix matrix = new Matrix();
            // RESIZE THE BIT MAP
            matrix.postScale(scaleWidth, scaleHeight);

            // "RECREATE" THE NEW BITMAP
        final   Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width,
                    height, matrix, false);

            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    your_imageView.setImageBitmap(resizedBitmap);
                }
            })
        }
    }).start();
}

thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM