开发者

How to resize bitmap decoded from URL?

开发者 https://www.devze.com 2023-03-29 08:26 出处:网络
I am using bitmap to get a image from a url using this public void loadImage(String url){ try { bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());

I am using bitmap to get a image from a url using this

public void loadImage(String url){
try {

       bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());

    } catch (MalformedURLException e) {
      e.printStackT开发者_Go百科race();
    } catch (IOException e) {
      e.printStackTrace();
    }

}

Is there anyway i can resize the image from here? setting the width and the height of it still keeping the resolution?


Use: Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)


You can try this too.

BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1; // 1 = 100% if you write 4 means 1/4 = 25% 
bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent(),
                                                                   null, bmOptions);
bmImage.setImageBitmap(bitmap);


Maybe this will help you:

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;
}


You can just use Picasso(very simple!):

Picasso.with(context) .load(url) .resize(10, 10) .into(imageView)

http://square.github.io/picasso/


why don't you set the width and height property of imageView in XML?

I find out that the image from url is resized automatically so that it can fit in that imageView.


I needed similar capabilities in my app also. I found the best solution for me here.

https://github.com/coomar2841/image-chooser-library/blob/dev/src/com/kbeanie/imagechooser/threads/MediaProcessorThread.java

You might not need all that functionality, but at some point the guy is compressing /scaling bitmaps.

0

精彩评论

暂无评论...
验证码 换一张
取 消