-1

i am building a chat application using firebase. I am using Glide to display images

Glide.with(getApplicationContext())
                        .load(imageUrl)
                        .crossFade()
                        .fitCenter()
                        .placeholder(R.drawable.loading)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(imageView);

I want to save the images to a specific folder in Internal Storage just like whatsapp does and load images from there after it has been saved.Images are uploaded on Firebase Storage and its URL is saved in Firebase Database and i load them in a imageView with the url using Glide

Paramjeet Singh
  • 2,930
  • 2
  • 9
  • 17

1 Answers1

4
    private static final String IMAGE_DIRECTORY = "/yourfoldername/images";

    //this method return your folder image path
    public String saveImage(Bitmap myBitmap) {
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            File wallpaperDirectory = new File(
                    Environment.getExternalStorageDirectory() + IMAGE_DIRECTORY);
            // have the object build the directory structure, if needed.
            if (!wallpaperDirectory.exists()) {
                wallpaperDirectory.mkdirs();
            }
            try {
                File f = new File(wallpaperDirectory, Calendar.getInstance().getTimeInMillis() + ".jpg");
                f.createNewFile();
                FileOutputStream fo = new FileOutputStream(f);
                fo.write(bytes.toByteArray());
                MediaScannerConnection.scanFile(this,
                        new String[]{f.getPath()},
                        new String[]{"image/jpeg"}, null);
                fo.close();
                Log.d("TAG", "File Saved::--->" + f.getAbsolutePath());

                return f.getAbsolutePath();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            return "";
        }


    // glide load image
    Glide.with(this)
        .load(imageUrl)
        .asBitmap()
        .into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {

                String imagepath = saveImage(resource);
// Parse the gallery image url to uri
                Uri savedImageURI = Uri.parse(imagepath);



// Display the saved image to ImageView
            iv_saved.setImageURI(savedImageURI);

            }
        });
Deep
  • 162
  • 7
  • where will this code load the image there is no mention fior any imageView, i want to load image in a imageView – Paramjeet Singh Apr 19 '18 at 05:17
  • glide onResourceReady method provide Bitmap so, you can use this line imageview.setImageBitmap(resource); – Deep Apr 19 '18 at 05:22
  • Will the image load from internal storage, i want it to be loaded from internal storage once it is downloaded every time user opens the Activity. – Paramjeet Singh Apr 19 '18 at 05:28
  • If i will use the same code, Image will downloaded and saved every time user opens the activity but i want it to happen only once, any solution. I m – Paramjeet Singh Apr 19 '18 at 05:48