I have healthy working code to share single image from my app's internal memory using FileProvider facility given in android-support-v4.jar library.
Here is the code snippet
Uri uri = FileProvider.getUriForFile(MSafeGalleryActivity.this, "com.packagename.fileprovider", file);
final Intent intent = ShareCompat.IntentBuilder.from(SafeGalleryActivity.this)
.setType("image/jpeg")
.setStream(uri)
.setChooserTitle("Share with")
.createChooserIntent()
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
If you see the code it accept only one URI in setStream() method.
So Please can you help me out here to share multiple images from my app's internal memory.
To share multiple images, I use the following code:
ArrayList<Uri> imageUris = new ArrayList<>();
// Put your file uri in imageUris
// ...
// Create intent
Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
// Grant permission to read files
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("image/*");
// Start chooser activity
startActivity(Intent.createChooser(shareIntent, getString(R.string.share_send)))
Related
Instagram allows a single image or video to be uploaded programmatically from an Android app via Android Intents. I have been able to do this successfully. What I want to know is it possible for Instagram to handle multiple images using Intents? Not much to no information on this unfortunately. The following is my last attempt which opens Instagram briefly then closes with a toast message saying "Unable to load image".
Have tried both Intent.ACTION_SEND and Intent.ACTION_SEND_MULTIPLE
val fileUris = ArrayList<Uri>()
val newFile = File("/data/user/0/com.myapp.android/files/media/961087022.jpg")
val contentUri = getUriForFile(this, "com.myapp.fileprovider", newFile)
grantUriPermission("com.instagram.android", contentUri, FLAG_GRANT_READ_URI_PERMISSION)
fileUris.add(contentUri)
val newFile2 = File("/data/user/0/com.myapp.android/files/media/961146948.jpg")
val contentUri2 = getUriForFile(this, "com.myapp.fileprovider", newFile2)
grantUriPermission("com.instagram.android", contentUri2, FLAG_GRANT_READ_URI_PERMISSION)
fileUris.add(contentUri2)
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "image/*"
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, fileUris)
shareIntent.`package` = "com.instagram.android"
startActivity(Intent.createChooser(shareIntent, "Share to"))
I was looking on how to share multiple video files to instagram stories.. I couldn't find how to do it reading the facebook documentation.
Instead i set the intent to send multiple files and set the intent package to "com.instagram.android", and surprisingly it worked..
Intent share = new Intent(Intent.ACTION_SEND_MULTIPLE) ;
share.setType("video/*");
share.putExtra(Intent.EXTRA_SUBJECT, "abc");
share.putExtra(Intent.EXTRA_TITLE, "abcd");
ArrayList<Uri> files = new ArrayList<Uri>();
for (String path : filesToSend) {
File myFiles = new File(path);
Uri doneUri = FileProvider.getUriForFile(Objects.requireNonNull(getApplicationContext()),
BuildConfig.APPLICATION_ID + ".provider", myFiles);
files.add(doneUri);
}
share.setPackage("com.instagram.android");
share.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);
startActivity(share);
Hope, this helps!!
I'm trying to implement a share button that captures a screenshot and shares it through the standard Android interface. I'm able to create the screenshot (and I can see it when I browse the SD card), but when I try to send it, the messages app gives the error: "messaging failed to upload attachment."
My code
File imageDir = new File(Environment.getExternalStorageDirectory(), "inPin");
if(!imageDir.exists()) { imageDir.mkdirs(); }
File closeupImageFile = new File(imageDir, "closeup.png");
File overviewImageFile = new File(imageDir, "overview.png");
View mapView = findViewById(R.id.floor_map);
saveScreenshotToFile(mapView, closeupImageFile);
ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(Uri.fromFile(closeupImageFile));
String message = String.format("I'm on %s %s", building.name, getCurrentFloor().name);
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.setType("image/*");
startActivity(Intent.createChooser(sendIntent, "Share via"));
The saveScreenshotToFile method
private static void saveScreenshotToFile(View view, File saveFile) throws IOException {
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap bitmap = view.getDrawingCache();
FileOutputStream out = new FileOutputStream(saveFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.close();
}
I'm using Android Marshmallow, API level 23, on the emulator—I don't know if this makes a difference, but I was able to share using other apps on the emulator and it worked fine.
You are sending File URIs, with which Intent.FLAG_GRANT_READ_URI_PERMISSION does not apply and should not be used, as explained in this video and its accompanying blog post. Other apps would need the storage permission to access your files.
From the blog post:
Instead, you can use URI permissions to grant other apps access to specific Uris. While URI permissions don’t work on file:// URIs as is generated by Uri.fromFile(), they do work on Uris associated with Content Providers. Rather than implement your own just for this, you can and should use FileProvider as explained in the File Sharing Training.
If you have to send MMS with any Image using Intent then use this code.
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
sendIntent.putExtra("sms_body", "some text");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/image_4.png"));
sendIntent.setType("image/png");
startActivity(sendIntent);
OR
If you have to send MMS with Audio or Video file using Intent then use this.
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
sendIntent.putExtra("address", "1213123123");
sendIntent.putExtra("sms_body", "if you are sending text");
final File file1 = new File(mFileName);
if(file1.exists()){
System.out.println("file is exist");
}
Uri uri = Uri.fromFile(file1);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.setType("video/*");
startActivity(sendIntent);
I would love to share a picture with a text or url.
I'm using this code but I am only sharing the image, and changing the order of the "type" makes me share both but only as email / gmail
What am I doing wrong? my code is:
edit1
Intent share = new Intent(Intent.ACTION_SEND_MULTIPLE);
share.setType("*/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Wallpaper/1.jpg"));
share.putExtra(Intent.EXTRA_TEXT, "helloworld");
startActivity(Intent.createChooser(share, (getApplicationContext().getString(R.string.condividi))));
try this
File DoutFile = new File(path);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(DoutFile));
share.putExtra(Intent.EXTRA_TEXT,
"" + getResources().getString(R.string.app_name));
startActivity(Intent.createChooser(share, "Share Image"));
Specify MIME type also for the text. "text/plain" is the type of text data MIME. Try using "*/*" as MIME, so you can send any generic data type.
Also try changing ACTION_SEND to ACTION_SEND_MULTIPLE which specialized for delivering multiple data.
More info about ACTION_SEND_MULTPLE and handling MIME types:
http://developer.android.com/reference/android/content/Intent.html
If you want to send multiple files then use the following code :-
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra(Intent.EXTRA_SUBJECT, "Add any subject");
intent.setType("image/*"); /* sharing any type of image. Modify to your need */
ArrayList<Uri> files = new ArrayList<Uri>();
for(String path : filesToSend /* List of images you want to send */) {
File file = new File(path);
Uri uri = Uri.fromFile(file);
files.add(uri);
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files); /* adding files to intent */
startActivity(intent);
Actually I want to share image in Instagram through intent.
I found this solution for images saved on SD card but I want to do same for image on site (link).
I tried with
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent
.putExtra(
Intent.EXTRA_STREAM,
Uri.parse("http://www.alliswell.biz/images/products/art/alliswell_signs/yellowB.jpg"));
shareIntent.setPackage("com.instagram.android");
startActivity(shareIntent);
But it's not working.
Edit
When I start above intent it opens my installed Instagram application and it immediately finish Instagram and toast message comes "unable to download file"
Actually it does not parse link and image respectively. What should be issue?
You should use local path to file
For example: "file:///path/to/file/image.png".
Note, that it is very important to include "file" in the path, without this part it also can show the same toast.
First of all you need to download file from that url. you may refer this code for downloading image from url:
String imageUrl = "Your_Image_Url";
if (imageUrl != null && !imageUrl.equals("")) {
String fileName = generateFileNameFromUrl(imageUrl);
String imageLocalPath = Environment.getExternalStorageDirectory()+ File.separator+"Your_App_Name"+ fileName;
if (!new File(imageLocalPath).exists()) {
ImageDownloadModel imageDownloadModel = new ImageDownloadModel();
imageDownloadModel.setImageLocalPath(imageLocalPath);
imageDownloadModel.setImageUrl(imageUrl);
imageDownloadModels.add(imageDownloadModel);
}
ImageLoadAsynkTask imageLoadAsynkTask = new ImageLoadAsynkTask(new ImageDownloadDelegate(), imageDownloadModels, albumDir, activity);
imageLoadAsynkTask.execute();
and then use uri for that image for sharing it on instagram:
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + imageLocalPath));
shareIntent.setPackage("com.instagram.android");
activity.startActivity(shareIntent);
How can I share images using intent chooser. I have tried
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
share.putExtra(Intent.EXTRA_STREAM,
Uri.parse(url));
where url is from internet
I am able to share text using the above code the image doesnt get attached.
try doing this instead i think it will work
Uri uri = Uri.fromFile(new File(filename));
share.putExtra(Intent.EXTRA_STREAM, uri);