Skip to content Skip to sidebar Skip to footer

Converting Image Url To Bitmap Quickly

I need to display the list of images from api in the list page. For that i used two approaches. First Approach: By converting the url to byte array and then converting it into bitm

Solution 1:

There is a library named Picasso. which can efficiently load images from url. it can also load image from the File. all you wanted to do , write a line of code.

example

Picasso.with(context) //Context
    .load("http://i.imgur.com/DvpvklR.png") //URL/FILE
    .into(imageView)//an ImageView Object to show the loaded image; 

It can also cache your image, so the loaded image could be able to load faster on the next time without wasting the data.

There are many more options available in Picasso. Here is the documentation

If you need rounded cornered bitmap

Picasso.with(mContext)
    .load("your-image-url-or-file-or-drawable")
    .transform(new RoundedTransformation(200, 0))
    .fit()
    .into(imageView);

RoundedTransformation.java

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;

// enables hardware accelerated rounded corners// original idea here : http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/publicclassRoundedTransformationimplementscom.squareup.picasso.Transformation {
    privatefinalint radius;
    privatefinalint margin;  // dp// radius is corner radii in dp// margin is the board in dppublicRoundedTransformation(finalint radius, finalint margin) {
        this.radius = radius;
        this.margin = margin;
    }

    @Overridepublic Bitmap transform(final Bitmap source) {
        finalPaintpaint=newPaint();
        paint.setAntiAlias(true);
        paint.setShader(newBitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));

        Bitmapoutput= Bitmap.createBitmap(source.getWidth(), source.getHeight(), Config.ARGB_8888);
        Canvascanvas=newCanvas(output);
        canvas.drawRoundRect(newRectF(margin, margin, source.getWidth() - margin, source.getHeight() - margin), radius, radius, paint);

        if (source != output) {
            source.recycle();
        }

        return output;
    }

    @Overridepublic String key() {
        return"rounded";
    }
}

Solution 2:

There are open-source libraries which focus on loading image into an ImageView. Take for example of universal-image-loader, it is very easy to use, like:

// Load image, decode it to Bitmap and display Bitmap in ImageView (or any other view //  which implements ImageAware interface)
imageLoader.displayImage(imageUri, imageView);

or:

// Load image, decode it to Bitmap and return Bitmap to callback
imageLoader.loadImage(imageUri, newSimpleImageLoadingListener() {
    @OverridepublicvoidonLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        // Do whatever you want with Bitmap
    }
});

or:

// Load image, decode it to Bitmap and return Bitmap synchronouslyBitmapbmp= imageLoader.loadImageSync(imageUri);

Take example of Volley, you can use it like this:

publicvoiddisplayImg(View view){
    ImageViewimageView= (ImageView)this.findViewById(R.id.image_view);
    RequestQueuemQueue= Volley.newRequestQueue(getApplicationContext()); 

    ImageLoaderimageLoader=newImageLoader(mQueue, newBitmapCache());

    ImageListenerlistener= ImageLoader.getImageListener(imageView,R.drawable.default_image, R.drawable.default_image);
    imageLoader.get("http://developer.android.com/images/home/aw_dac.png", listener);
    //指定图片允许的最大宽度和高度//imageLoader.get("http://developer.android.com/images/home/aw_dac.png",listener, 200, 200);
}

These libraries are used broadly, and more importantly, they are open-sourced. No need to implement functions like this repeatedly.

Solution 3:

Try this

Hope this should help you.

Post a Comment for "Converting Image Url To Bitmap Quickly"