How to share the image file from your own app through Instagram, Whatsapp or maybe any social app?

how to share file in android programmatically
how to share video in android programmatically

I wanted to share images with your own app to all "Instagram", "WhatsApp" or maybe using any social media app?

As I got the solution for whatsapp, I still couldn't get the solution for Instagram.

My code is as follows:

share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
            Bitmap bitmap = drawable.getBitmap();

            Intent share = new Intent(Intent.ACTION_SEND);
            share.setType("image/jpeg");
            share.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            ByteArrayOutputStream bytes = new ByteArrayOutputStream();

            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

            Random generator = new Random();
            int n = 10000;
            n = generator.nextInt(n);

            File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temp_"+n+".jpg");

            try {
                f.createNewFile();
                FileOutputStream fo = new FileOutputStream(f);
                fo.write(bytes.toByteArray());
                fo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            share.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().getPath()));

            String savedFile = saveImageFile(bitmap, "myFolder");
            Uri imageUri =  Uri.parse(savedFile);

            share.putExtra(Intent.EXTRA_STREAM, imageUri);
            startActivity(Intent.createChooser(share, "Share Image"));
        }
    });
}

savedImages method:-

public static String saveImageFile(Bitmap image, String folder){

        String now = Long.toString(new Date().getTime());

        File imageFile = new File(Environment.getExternalStorageDirectory()+"/" + folder);
        if (!imageFile.exists()){
            File screenShotsFolder = new File(Environment.getExternalStorageDirectory()+"/"+ folder+ "/");
            screenShotsFolder.mkdirs();
        }

        File imageName = new File(new File(Environment.getExternalStorageDirectory() +"/"+ folder + "/"), now + ".jpg" );

        try{
            FileOutputStream outputStream = new FileOutputStream(imageName);
            image.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            outputStream.flush();
            outputStream.close();
        }catch (Throwable e){e.printStackTrace();}
        return imageName.toString();
    }

This code is working for "WhatsApp" but not for "Instagram"

In Instagram, I am not able to send to particular person like "9gag".

Here is the complete code. Change your code to the following

share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
            Bitmap bitmap = drawable.getBitmap();

            Intent share = new Intent(Intent.ACTION_SEND);
            share.setType("image/jpeg");

            ByteArrayOutputStream bytes = new ByteArrayOutputStream();

            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

            String savedFile = saveImageFile(bitmap, "myFolder");

            Uri imageUri =  Uri.parse(savedFile);
            share.putExtra(Intent.EXTRA_STREAM, imageUri);
            startActivity(Intent.createChooser(share, "Share Image"));

        }
    });

Saving Image file function

public static String saveImageFile(Bitmap image, String folder){

    String now = Long.toString(new Date().getTime());

    File imageFile = new File(Environment.getExternalStorageDirectory() + folder);
    if (!imageFile.exists()){
        File screenShotsFolder = new File("/sdcard/Pictures/" + folder+ "/");
        screenShotsFolder.mkdirs();
    }

    File imageName = new File(new File("/sdcard/Pictures/" + folder + "/"), now + ".jpg" );

    try{
        FileOutputStream outputStream = new FileOutputStream(imageName);
        image.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        outputStream.flush();
        outputStream.close();
    }catch (Throwable e){e.printStackTrace();}
    return imageName.toString();
}

java, Here is the complete code. Change your code to the following share.​setOnClickListener(new View.OnClickListener() { @Override public void onClick(​View v)  Step 1: Open web.whatsapp.com on your PC. You will see a QR code there. Step 2: Open WhatsApp on your mobile device and tap the three-dot icon at the top-right corner. Select WhatsApp web from the list. Step 3: Using your phone, scan the code on your PC.

Use,

  share.putExtra(Intent.EXTRA_STREAM, Uri.parse(f.getPath()));

instead of Environment.getExternalStorageDirectory().getPath())

but for nougat and above you should use FileProvider to get the file uri..

  picUri = FileProvider.getUriForFile(RegisterActivity.this , this.getApplicationContext().getPackageName() + ".provider", f);

Sharing to Other Social Networks, How do I get a link (URL) to a photo or video on Instagram? You can only get a link When I share an Instagram post to Facebook, is it visible to the public? No. Can I change the name of the Facebook album I upload Instagram posts to? You can How do I disconnect my Instagram account from another social network? Sharing buttons: On every image page on Imgur, there are a series of small icons for sharing that image on your favorite social networks, including Facebook, Pinterest, Google+, Tumblr, Reddit and many more. Click on the corresponding icon to share on your social media platform of choice.

Prefer this File share in Naught or above (API level 28 or above)

How do I share from Instagram to other social networks?, Tap Account > Linked Accounts, then tap a social network to log in and link the When using Hipstamatic, you can share your photo directly to Instagram. you to the app if it's already installed on your phone or to the Hipstamatic App now see Weibo as one of the available services. Was this information helpful? Yes. No. I tested 2.x Mb images and even that seemed to work 9/10 times. <300KB is the safest approach, but you should be fine using larger images as of 18-02-2020. I would recommend keeping the file size below 2MB, though. And definitely throw your image through TinyPNG or any other image compression algorithm if you haven't already.

Data Policy, The types of information we collect depend on how you use our Products. strength, available storage space, browser type, app and file names and types, and plugins. through Facebook Business Tools they use, including our social to collect, use and share your data before providing any data to us. Click on the received message to open Download file popup. Choose GIF or any file and send it to your WhatsApp contacts without limit. Method 4: Maybe you are not going to like this method but I have to share cause you can also use this method to send gifs in WhatsApp. Download Convert GIF to Video & Share. Open any GIF from your phone in this app.

Instagram, Bringing you closer to the people and things you love. — Instagram from Facebook Connect with friends, share what you're up to, or see what's new from others  Repost for Instagram + is one such app that is free to use. Find the post you want to regram. Click on the three dots in the upper right corner of the post. Open up the Repost for Instagram + app. The post will appear. You’ll have a choice on which corner to place the repost overlay.

Unfold, Unfold is the toolkit for storytellers. Create, edit and share beautiful stories with templates, text, fonts and filters. Create stories by choosing from 200+ stunning  Step 3. Mash the two files together in Artwork.fm. Place your mp3 file and your image file in the same folder on your computer, then go to Artwork.fm and select both the files to create your artwork video. Download your video and email it to yourself. If it’s for Instagram, it should be less than 1mb so easily sent through Gmail. Step 4. Publish

Comments
  • Thanks Lucern..., It worked for whatsapp, but it is not working for instagram though...
  • Thanks @Santanu Sur, but I am receiving an null pointer exception after using FileProvider, but I just couldn't understand why am I receiving it??
  • Thanks @Harshil kakadiya
  • Its my pleasure..!!