[JAVA] [Android] How to pass images and receive callbacks when sharing using ShareCompat

↓ This thing

I felt that there was a lot of information, but it didn't come out unexpectedly, so I wrote a memo.




//Prerequisite code to run from within the activity
private void showShareChooser() {
	File tempFile = new File(getApplicationContext().getExternalCacheDir(), tempImgFilePath);

	//If you want to get the URI to share the file, you need to get it by specifying the name of the authority described later through FileProvider.
	Uri uri = FileProvider.getUriForFile(getApplicationContext()
			, getApplicationContext().getPackageName() + ".provider"
			, tempFile);

	ShareCompat.IntentBuilder builder = ShareCompat.IntentBuilder.from(this);
	builder.setChooserTitle(chooserTitle) //Title when sharing
			.setSubject(subject) //subject. How it is used depends on the shared app
			.setText(text) //Text. How it is used depends on the shared app
			.setStream(uri) //When sharing a file, specify its URI
			.setType("image/jpeg"); //MIME type of the file specified in the stream

	//Grant read permission on the URI
	Intent intent = builder.createChooserIntent().addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

	//If you want to receive a callback, use that intent to start the activity
	if (intent.resolveActivity(getPackageManager()) != null) {
		startActivityForResult(intent, SNS_SHARE);

	//If you don't need to receive the results, you can just start from the builder
	// builder.startChooser();

If you want to receive the return, you can receive it with ʻonActivityResult` as usual.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
	switch (requestCode) {
		case SNS_SHARE:
			//Receive the return and do something
			//resultCode is always zero, so RESULT_Do not judge with OK

			super.onActivityResult(requestCode, resultCode, data);

When sharing files

When sharing images etc., Bitmap cannot be shared as it is, so it is necessary to save it to a file once. In addition, it is necessary to allow reading from the save destination as follows.

First, specify the path in XML.


<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-cache-path name="cache" path="." />

ʻExternal-cache-path etc. changes depending on the save destination. Although it is in English, see [Official FileProvider Reference](https://developer.android.com/reference/android/support/v4/content/FileProvider). Then specify that provider in ʻAndroidManifest.xml.




Recommended Posts

[Android] How to pass images and receive callbacks when sharing using ShareCompat
How to test including images when using ActiveStorage and Faker
[Rails] How to upload images to AWS S3 using Carrierwave and fog-aws
[Rails] How to upload images to AWS S3 using refile and refile-s3
[Rails] How to upload images using Carrierwave
[Android] How to turn the Notification panel on and off using StatusBarManager
How to output Excel and PDF using Excella
POST images from Android to PHP using Retrofit
How to execute and mock methods using JUnit
How to play audio and music using javascript
[Rails] How to upload multiple images using Carrierwave
How to use scope and pass processing (Jakarta)
How to link images using FactoryBot Active Storage
How to add characters to display when using link_to method
How to remove Ubuntu when dual booting Windows and Ubuntu
How to convert A to a and a to A using AND and OR in Java
How to set environment variables when using Payjp with Rails
How to easily implement in-app purchase using itemstore <Implementation: Android>
How to use OpenCV 4 on Android and view camera live view
Android app to select and display images from the gallery
How to input multiple images at once using rake task
How to set and describe environment variables using Rails zsh
How to write and notes when migrating from VB to JAVA
[2020 version] How to send an email using Android Studio Javamail
[Rails] How to delete images uploaded by carrierwave (using devise)
How to make an app using Tensorflow with Android Studio
How to exclude auto-generated from Jacoco coverage when using Lombok
How to join a table without using DBFlute and sql
How to write query option when using gem ruby-firebase (memorial)
I wanted to animate a row when using realm and RecyclerView on Android, but I gave up
[rails] How to post images
How to handle uploaded images
How to minimize Java images
How to authorize using graphql-ruby
How to POST JSON in Java-Method using OkHttp3 and method using HttpUrlConnection-
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
[Android] Convert Map to JSON using GSON in Kotlin and Java
How to solve the unknown error when using slf4j in Java
[Swift5] How to communicate from ViewController to Model and pass a value
[Rails 5] How to display the password change screen when using devise
[rails6.0.0] How to save images using Active Storage in wizard format
How to narrow down the image format from the gallery on Android and then select and import multiple images