Bitmap image compress from url

社会主义新天地 提交于 2019-12-11 18:52:50

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/24797648/bitmap-image-compress-from-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!