Use ShareCompat.IntentBuilder to share an attachment

This way the share sheet will show a preview when sharing images.
pull/209/head
cketti 3 years ago
parent 394ee99b56
commit eb9b0ed10a

@ -41,6 +41,7 @@ import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ShareCompat;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Lifecycle;
@ -271,12 +272,10 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, file); Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, file);
// TODO: consider using getUriForFile(..., displayName) // TODO: consider using getUriForFile(..., displayName)
Intent send = new Intent(); new ShareCompat.IntentBuilder(context)
send.setAction(Intent.ACTION_SEND); .setType(attachment.getMimeType())
send.putExtra(Intent.EXTRA_STREAM, uri); .addStream(uri)
send.setType(attachment.getMimeType()); .startChooser();
send.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(send, context.getString(R.string.title_select_app)));
return true; return true;
} catch (Throwable ex) { } catch (Throwable ex) {

@ -116,6 +116,7 @@ import androidx.appcompat.widget.PopupMenu;
import androidx.constraintlayout.helper.widget.Flow; import androidx.constraintlayout.helper.widget.Flow;
import androidx.constraintlayout.widget.ConstraintLayout; import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.Group; import androidx.constraintlayout.widget.Group;
import androidx.core.app.ShareCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
import androidx.core.content.pm.ShortcutInfoCompat; import androidx.core.content.pm.ShortcutInfoCompat;
@ -3984,12 +3985,12 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
if (uris == null) if (uris == null)
return; return;
final Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE); ShareCompat.IntentBuilder shareIntentBuilder = new ShareCompat.IntentBuilder(context);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); shareIntentBuilder.setType("image/*");
intent.setType("image/*"); for (Uri uri: uris) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); shareIntentBuilder.addStream(uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); }
context.startActivity(Intent.createChooser(intent, context.getString(R.string.app_name))); shareIntentBuilder.startChooser();
} }
@Override @Override

Loading…
Cancel
Save