Mohit Kondur
Mohit Kondur

Reputation: 11

android code to select a file and then share it through whatsapp

I am trying to create an application that selects a file from the device file manager. Upon selecting the file it should be shared via WhatsApp.
Can someone please help on how to select a file and then share that selected file?

  1. How to get the path of the file?
  2. How to use that path for sharing it through WhatsApp?
  3. Is there any code to be added to the manifest.xml file?

The xml layout has two buttons. One button to browse into the file manager and the other button to share the selected file. It also has a TextView to display the path.

public class MainActivity extends AppCompatActivity {
    Button b,button;
    TextView t;
    Intent intent;
    public String PathHolder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    b=(Button) findViewById(R.id.b);



    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {



            File sharingfile = new File(PathHolder);

            Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
            whatsappIntent.setType("image/jpg");//mention type of image you want to share
            whatsappIntent.setPackage("com.whatsapp");
            Uri uri = Uri.fromFile(sharingfile);
            whatsappIntent.putExtra(Intent.EXTRA_STREAM, uri);
            try {
                startActivity(whatsappIntent);
            } catch (android.content.ActivityNotFoundException ex) {

                Toast.makeText(getApplicationContext(),"Whatsap not installed",Toast.LENGTH_SHORT).show();
            }



        }
    });

        button = (Button)findViewById(R.id.button) ;
        t=(TextView)findViewById(R.id.textView);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*");
                startActivityForResult(intent, 7);

            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub

        switch(requestCode){

            case 7:

                if(resultCode==RESULT_OK){

                    PathHolder = data.getData().getPath();

                    Toast.makeText(MainActivity.this, PathHolder , Toast.LENGTH_LONG).show();
                    t.setText(PathHolder);
                }
                break;

        }
    }
}

yet I am unable to share a file from the device through WhatsApp.

Upvotes: 1

Views: 201

Answers (1)

Grisgram
Grisgram

Reputation: 3243

What you are looking for is the Intent.createChooser method to share content of an URI with other registered Apps that can handle it.

It's the same mechanic for sending Emails, sharing images or links, and many other tasks.

This article should help you: https://developer.android.com/training/sharing/send

This is the official doc

Intent.createChooser

public static Intent createChooser (Intent target, CharSequence title)

Added in API level 1

Convenience function for creating a ACTION_CHOOSER Intent.

Builds a new ACTION_CHOOSER Intent that wraps the given target intent, also optionally supplying a title. If the target intent has specified FLAG_GRANT_READ_URI_PERMISSION or FLAG_GRANT_WRITE_URI_PERMISSION, then these flags will also be set in the returned chooser intent, with its ClipData set appropriately: either a direct reflection of getClipData() if that is non-null, or a new ClipData built from getData().

Parameters

target

The Intent that the user will be selecting an activity to perform.

title

Optional title that will be displayed in the chooser.

Returns

Return a new Intent object that you can hand to Context.startActivity() and related methods.

Upvotes: 0

Related Questions