简体   繁体   中英

How to compress drawable image that loaded with Glide

i have this code here and i want to compress drawable result how can i do that?

 Glide.with(context)
                .load(Urls.BASE_URI +items.get(holder.getAdapterPosition()).getUserPhotoUrl())
                .apply(requestOptions
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .skipMemoryCache(true).dontAnimate().fitCenter().circleCrop().override(100,100)
                )
                .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        holder.userPhoto.setImageDrawable(resource);

                    }
                });

try this

then write this code to get the drawable bitmap then convert the bitmap to file

public void doTheJob(){
Bitmap bitmap= BitmapFactory.decodeResource(context.getResources(),
                                           R.drawable.icon_resource);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

//you can create a new file name "test.jpg" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory()
                        + File.separator + "test.jpg");
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());


new Compressor(this).compressToFileAsFlowable(f)
                       .subscribeOn(Schedulers.io())
                       .observeOn(AndroidSchedulers.mainThread())
                       .subscribe(file -> getTheBitmapOfTheFile(file), throwable -> throwable.printStackTrace());
// remember close de FileOutput
fo.close();
}

public void getTheBitmapOfTheFile(File file){
        Bitmap bitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        try {
            bitmap = BitmapFactory.decodeStream(new FileInputStream(file), null, options);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
}

but please don't forget to the permission of reading and writing to external storage

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