Android Intent share Image from url path without saving in local - android

Hi I need to share image using intent the image is stored in server path ho to achieve it
private void shareContent(String urlpath)
{
URL url = null; //Some instantiated URL object
try {
url = new URL(urlpath);
URI uri = url.toURI();
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
URI screenshotUri = uri;
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
context.startActivity(Intent.createChooser(sharingIntent, "Share image using"));
} catch (Exception e) {
e.printStackTrace();
}
}
This is the method that I am using when share button is clicked and I am working in this list adapter

The documentation for EXTRA_STREAM states that the value needs to be a content URI. Technically nothing is stopping you from putting a http URI there. But 99% of receiving apps won't support that.
What you could do is write a ContentProvider that accesses the contents of a http URI and streams them to the receiving application. Kind of like FileProvider, but for http URIs instead of files.
Or, prior to sharing, you download the image, write it to a local file and expose it using FileProvider.

Related

Android: Image resolution difference when sharing through Intent.EXTRA_STREAM

I have a menu option to share my app which generates an ArrayList of Uris object with a list of drawables Uris, and my issue is I've saved all the screenshots in 540x1170px and they get to email in 1620x3510px (curiously? exactly the triple), and in the mail they look too big.
This next is the code I use to share the images, and I'd like to know the reason why the original resolution is altered, and if there is a way to set intent image resolution.
public static void shareFile(ArrayList<Uri> uris, String fileType)
{
try{
Intent share = new Intent(Intent.ACTION_SEND_MULTIPLE);
share.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
share.setType(fileType);
String strSubject = TMLocale.getStringResourceByName("mainmenu_sharethisapp_subject");
share.putExtra(Intent.EXTRA_SUBJECT, strSubject);
share.putExtra(Intent.EXTRA_TEXT, TMLocale.getStringResourceByName("mainmenu_sharethisapp_recommendtext"));
share.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
activity.get().startActivity(Intent.createChooser(share, TMLocale.getStringResourceByName("mainmenu_sharethisapp_shareapptitle")));
}
catch(Exception ex){
Toast.makeText(activity.get(), String.valueOf(ex.getMessage()),Toast.LENGTH_LONG).show();
}
}
And I call it setting "image/jpg" as fileType.

I want to make android application to send picture, message to instagram users using instagram API

I've tried several sources from Google and GitHub but didn't find any authentic source that could help me sending multiple pictures and posts automatically from my android gallery using the scheduler. Is anyone working with the Instagram API? If so, could you please give me some authentic source?
Instead of using the Instagram API, you can directly start an Intent to post an Image or Video to Instagram by opening a "Share with" dialog. This will require user interaction.
String type = "image/*";
String filename = "/myPhoto.jpg";
String mediaPath = Environment.getExternalStorageDirectory() + filename;
private void createInstagramIntent(String type, String mediaPath){
// Create the new Intent using the 'Send' action.
Intent share = new Intent(Intent.ACTION_SEND);
// Set the MIME type
share.setType(type);
// Create the URI from the media
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);
// Add the URI to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
// Broadcast the Intent.
startActivity(Intent.createChooser(share, "Share to"));
}
Code example taken from https://www.instagram.com/developer/mobile-sharing/android-intents/

How can i send either an image or a video file using ACTION_SEND?

I want to share either an image or a video file using ACTION_SEND. So basically when the users taps on an image and selects "share image/video" it should send either the image selected or the video selected.
here is my code i am using:
if (filep != null) {
}
File sending=new File(filep);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_SEND);
intent.setDataAndType(Uri.fromFile(sending),getMimeType(sending.getAbsolutePath()));
intent.putExtra(Intent.EXTRA_STREAM, sending);
startActivity(Intent.createChooser(intent , "Share"));
}
private String getMimeType(String url)
{
String parts[]=url.split("\\.");
String extension=parts[parts.length-1];
String type = null;
if (extension != null) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
type = mime.getMimeTypeFromExtension(extension);
}
return type;
So when testing, it takes me to which app i want to use to share with i.e whatsapp, Facebook, email etc. And then when selecting either one, it then says "sharing failed, please try again." I can't seem to figure out why it doesn't work. However i have the same code to display either image or video file full screen with ACTION_VIEW and that seems to work great but not with sharing.
Can anyone assist please?
EXTRA_STREAM needs to be a Uri, and you are not passing a Uri. Use Uri.fromFile() to construct a Uri.
Also, replace setDataAndType() with setType(), as ACTION_SEND does not use the data aspect of the Intent.

File sharing using Intent.ACTION_SEND gives access denied on BBM

I am trying to share an audio file saved by my application. The file is added to the media store so all apps can access it.
Intent mediaScannerIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri fileContentUri = Uri.fromFile(finalFile);
mediaScannerIntent.setData(fileContentUri);
this.sendBroadcast(mediaScannerIntent);
I use Intent.ACTION_SEND to share the files to other apps:
public void shareRecording(View view) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.setType("audio/mp3");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///" + recording.getFilePath()));
try {
startActivity(Intent.createChooser(i, "Share " + recording.getName()));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "There are no app installed to share your audio file.", Toast.LENGTH_SHORT).show();
}
}
All working good with all apps like GMail, Yahoo mail, Whatsapp, ...
except BBM, it gives access denied.
What do I need to do to make it work on BBM ?
Thanks for any help.
UPDATE:
I used
Uri fileUri = Uri.parse("content://" + recording.getFilePath());
instead of
Uri fileUri = Uri.parse("file:///" + recording.getFilePath());
and it worked on BBM but not othe apps
So what is the difference between havin the URI parsed with: "file:///" and "content://" ?
And how can I used this to get the share working for all apps ?
The solution is to use the actual file to initialize the Uri object.
File recordingFile = new File(recording.getFilePath());
Uri fileUri = Uri.fromFile(recordingFile);
This worked with all the apps that can share a file.

share picture via Intent (Facebook and co)

I'm wasting a lot of time, trying to get sharing a simple jpeg image working via sharing Intent.
The most important one as usual is Facebook, and as usual it doesn't work.
Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri picUri = Uri.parse("http://someserver.com/somepic.jpg");
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_STREAM, picUri);
shareIntent.putExtra(Intent.EXTRA_TEXT, someString);
startActivity(Intent.createChooser(shareIntent, "some text..."));
The chooser opens nicely, Facebook also opens and makes me login, but then it tells me that updoad failed.
I also tried Flicker and Mail, and they all fail.
Then I tried to write the image to local file and send from there, also failed:
Drawable dr = ImageLoader.getDrawable(url);
Bitmap bmp = ((BitmapDrawable)dr).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 80, stream);
byte[] data = stream.toByteArray();
FileOutputStream ostream;
try {
ostream = this.openFileOutput("pic.jpeg", Context.MODE_WORLD_READABLE);
ostream.write(data);
} catch (Exception e) {
e.printStackTrace();
}
Uri picUri = Uri.fromFile(new File("pic.jpeg"));
shareIntent.putExtra(Intent.EXTRA_STREAM, picUri);
I've no clue if I do that correctly, haven't done that before.
My last try was to just send an HTML string with the image included as img tag. But Facebook seems not to handle type "text/html", so it's not an option.
I'm sure it needs only few lines of code. But which ones?
edit
I forgot the line
shareIntent.putExtra(Intent.EXTRA_STREAM, picUri);
in first code snippet. It was there when I tried, didn't work either.
No sleep for too long...
I think this will answer your question. https://stackoverflow.com/a/3553102/478537
It looks like you are on the right path, all I can see is that in your first code snippet, you are not using the picUri anywhere, and therefore its not being sent, and in the second, you are setting the EXTRA_STREAM twice (which wouldn't cause any issues, just redundant code).
..Intent.EXTRA_TEXT, someString.
This will fail your transaction with Facebook- not "someString"- Facebook sharing expects and searches for an URL transmitted over the EXTRA_TEXT. Why - don't ask..I did not understand the guys
Well, I spent a lot of time, and the issue was the extension of file (png made a problem so ignore the file extension in this case and use "jpg"), try the following code
public static void shareImageFileWithIntent(File imageFile, Context context) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
String type = mime.getMimeTypeFromExtension("jpg");
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType(type);
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
sharingIntent.putExtra(Intent.EXTRA_TEXT, "your extra text");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "your subject");
context.startActivity(Intent.createChooser(sharingIntent, context.getString(R.string.share_image_intent)));
}
Instead of passing image Via intent, you can create a new Class and save it there from one activity. And access that image from another activity.

Categories

Resources