1

I used glide 4.4.0 version.

I reviewed this link: How to load image through byte array using Glide?

But my question is different. I would like to directly byte array into imageView and it is possible Glide v4

https://github.com/bumptech/glide/blob/0cffd1da977e9ca334032ebc1d798213a177aab7/library/src/main/java/com/bumptech/glide/ModelTypes.java

 @NonNull
 @CheckResult
 T load(@Nullable byte[] model)

My question is how its caching image. It is provide that image loading is faster.

Please help me.

private void loadImageBytesWithGlide(byte[] imageBytes, final ImageView imageView) {

        glideRequests
                .load(imageBytes)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transition(withCrossFade())
                .into(imageView);

    }
Zoe
  • 27,060
  • 21
  • 118
  • 148
propoLis
  • 1,229
  • 1
  • 14
  • 48

2 Answers2

0

convert Base64 String to image here:

  .load(Base64.decode(imageBytes, Base64.DEFAULT))
Ege Kuzubasioglu
  • 5,991
  • 12
  • 49
  • 85
0

Create Image In sdcard from Base 64 string, then use Glide to loa image from path.

 if (theCaptchaBase64 != null) {
            byte[] image_data_bytes = Base64.decode(theCaptchaBase64, Base64.NO_WRAP);
            String Image_Data_str = null;
            try {
                Image_Data_str = new String(image_data_bytes, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            String imageFileExt = ".jpg";
            if (Image_Data_str != null && Image_Data_str.contains("png")) {
                imageFileExt = ".png";
            }


            long msTime = System.currentTimeMillis();
            Date dt = new Date(msTime);
            String format = "yyMMddHHmmssSSS";
            SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
            String captchaFileName = String.format("Captcha" + "_%s", sdf.format(dt).toString());
            //String captchaFileName = "Captcha";
            captchaFileName = captchaFileName + imageFileExt;

            String imageSaveFolderPath = TempFolderPath + "/" + captchaFileName;

            String imgPath = writeImageBytesOnSdCard(this, imageSaveFolderPath, Image_Data_str);

            loadImageWithGlide(this,ImageViiew,imgPath);
        }


         public static void loadImageWithGlide(Context theCtx, ImageView theImageView, String theUrl) {
        // 20170401 JK
        // skipMemoryCache, because it load the same image because URL not change.
        Glide.with(theCtx)
                .load(theUrl)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .into(theImageView);

        //Glide.with(mContext).load(imagePath).into(myHolder.mImageViewUserImage);
    }


        public static String writeImageBytesOnSdCard(Activity theActivity, String theCustomImagePath, String theCustomImageData) {
        String imagePath = theCustomImagePath;
        String temp[] = theCustomImageData.split("\\,");
        if (temp[0].contains("data:image/png")) {
            imagePath = getImagePath(imagePath, "png");
        } else {
            imagePath = getImagePath(imagePath, "jpg");
        }
        byte[] data = null;
        try {
            //data=myImageData1.getBytes("UTF-8");
            data = Base64.decode(temp[1], Base64.DEFAULT);
            FileOutputStream outputStream = null;
            outputStream = new FileOutputStream(new File(imagePath), false);
            InputStream in = new ByteArrayInputStream(data);

            OutputStream out = new BufferedOutputStream(outputStream);
            byte[] loginBuffer = new byte[1024];
            int byteRead = 0;
            while ((byteRead = in.read(loginBuffer)) != -1) {
                out.write(loginBuffer, 0, byteRead);
            }
            out.close();
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // code for sending a notification to the media scanner
            updateMedia(theActivity, imagePath);

        } catch (FileNotFoundException e) {
            imagePath = "";
            e.printStackTrace();
        } catch (IOException e) {
            imagePath = "";
            e.printStackTrace();
        }
        return imagePath;
    }

    public static String getImagePath(String theExportImagePath, String theRequiredExt) {
        File f = new File(theExportImagePath);
        String fileName = f.getName();

        String parent = f.getParent();
        String filename_Without_Ext = fileName;

        int dotposition = fileName.lastIndexOf(".");

        if (dotposition != -1)
            filename_Without_Ext = fileName.substring(0, dotposition);

        return parent + "/" + filename_Without_Ext + "." + theRequiredExt;
    }

    public static void updateMedia(final Activity theActivity, String filepath) {
        MediaScannerConnection.scanFile(theActivity.getApplicationContext(), new String[]{filepath}, null, null);

    }
jessica
  • 1,700
  • 1
  • 11
  • 17