Load bitmap with Glide

2

I am trying to load a Bitmap with the Glide library but there is no method to load it and in the documentation I did not find it either.

I've been looking for more sites like English EO and I found several posts but I did not get to understand well what they do. Let's see if you can help me.

I put you in situation: I need to load a Bitmap because the images I want to load in the ImageView I modify them to round them and put them as avatar and of course, when they are modified they remain as bitmap and I do not want to save them modified in the device.

All this is in a RecyclerView and I had planned to do it with Glide because if I do it normally with: ImageView.setImageBitmap() the RecyclerView is a bit lagged.

If someone has a solution other than to do it with Glide, it is also heard ^^

EDITED !!

This is what I do to assign the image and what I would like to change and do it with Glide

Bitmap bmp = BitmapFactory.decodeFile(ruta_imagen);
if (bmp != null) {
    viewHolder.imageAvatar.setImageBitmap(UtilidadesImagenes.getCircleBitmap(bmp);
}

This is the ViewHolder

public class ViewHolder extends RecyclerView.ViewHolder{

public ImageView imageAvatar;

    public ViewHolder(View v) {
         super(v);
         imageAvatar = (ImageView) v.findViewById(R.id.ivAvatar);
    }
}



public static Bitmap getCircleBitmap(Bitmap bitmap) {
    if(bitmap!=null){
        final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        final Canvas canvas = new Canvas(output);

        final int color = Color.RED;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawOval(rectF, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        bitmap.recycle();

        return output;
    }else{
        return null;
    }
}

The images that are loaded sometimes reach the 200KB that is when it begins to stay lagueado

    
asked by borjis 25.10.2016 в 11:57
source

3 answers

1

To upload an image using glide:

ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Glide
    .with(this)
    .load("ruta de la imagen o drawable")
    .centerCrop()
    .crossFade()
    .into(myImageView);
    
answered by 26.10.2016 в 10:23
1

Glide can receive a URI or a URL for loading images in the control you specify. In the following example I pass the URL of a bitmap that I generated when taking a photo.

 Glide.with(this)
.load(fileUri.getPath())
.diskCacheStrategy(DiskCacheStrategy.NONE)
.centerCrop()
.crossFade()
.into(imgPhoto);

If you do not have your bitmap saved, first you need to save it and get the route where you saved it

 public void saveBitmapToJPG(Bitmap bitmap, File photo) {
        Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(newBitmap);
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(bitmap, 0, 0, null);
        OutputStream stream = new FileOutputStream(photo);
        newBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        stream.close();
    }

and since you know that this is safe and you passed the route, you can get the URI and send it to glide

public static Uri getImageContentUri(Context context, File imageFile) {
        String filePath = imageFile.getAbsolutePath();
        Cursor cursor = context.getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new String[] { MediaStore.Images.Media._ID },
                MediaStore.Images.Media.DATA + "=? ",
                new String[] { filePath }, null);
        if (cursor != null && cursor.moveToFirst()) {
            int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
            cursor.close();
            return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + id);
        } else {
            if (imageFile.exists()) {
                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.DATA, filePath);
                return context.getContentResolver().insert(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            } else {
                return null;
            }
        }
    }
    
answered by 02.11.2016 в 23:49
0

Surely you will "lage" because the image is too heavy. Have you tried to resize the image before doing the ImageView.setImageBitmap() ?

int width = 120
int height = 120
imageView.setImageBitmap(Bitmap.createScaledBitmap(miImagenBitmap, width , height , false));
    
answered by 25.10.2016 в 12:34