Search code examples
androidandroid-glideandroid-intent-chooser

Share file using Glide


I'm trying to share GIF file. I want to share it via any social networking apps, I have a problem with file name. I don't how to get name file. This is my code:

llsetwallpapers.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // set as wallpapers
            lt.setText("Please Wait ...");
            lt.setTranslationY(100);
            lt.show();
            Glide.with(getApplicationContext())
                    .load(url)
                    .asBitmap()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(Bitmap resource, GlideAnimation<?super Bitmap> glideAnimation) {
                            File file = new File file = new File(getApplicationContext().getExternalCacheDir() , namefile)
                            file.setReadable(true);
                            Uri uri = Uri.fromFile(file);
                            Intent shareIntent = new Intent();
                            shareIntent.setAction(Intent.ACTION_SEND);
                            shareIntent.setType("image/gif");
                            shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
                            startActivity(Intent.createChooser(shareIntent, "Share with"));
                        }
                    });
        }
    });

Solution

  • The problem you are trying to solve is discussed at github issues, where Róbert Papp has provided an example implementation for sharing a file downloaded by Glide.

    The key part for you - is to get a reference to the File. That can be done with downloadOnly():

    
        File file = Glide
                        .with(context)
                        .load(url)
                        .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                        .get()
    
    

    Having a File, you can use FileProvider API in order to share the file.