Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share a file in Android programmatically

Tags:

android

share

I want to share a file(.pdf,.apk etc) using share Intent, I searched google but I find only the code for sharing Image

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse(path);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
like image 292
Renjith Krishnan Avatar asked Apr 12 '14 06:04

Renjith Krishnan


People also ask

How can I share data between two apps on Android?

Android uses the action ACTION_SEND to send data from one activity to another, even across process boundaries. You need to specify the data and its type. The system automatically identifies the compatible activities that can receive the data and displays them to the user.


2 Answers

You use the same code, you just change the MIME type for the type of data you wish to share. If you wish to share anything regardless of type, use */*

like image 103
Gabe Sechan Avatar answered Oct 06 '22 00:10

Gabe Sechan


To keep your code pragmatic, use ShareCompat class:

ShareCompat.IntentBuilder.from(this)
        .setStream(uri)
        .setType(URLConnection.guessContentTypeFromName(file.getName()))
        .startChooser();
like image 21
Chintan Soni Avatar answered Oct 05 '22 23:10

Chintan Soni