Android: Image resolution difference when sharing through Intent.EXTRA_STREAM - android

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.

Related

Sharing Keyboard image to WhatsApp Contact not shared directly

I have custom image keyboard app from which i am sharing image to any social media chatting application.
Now, As WhatsApp not supporting Commit content API to share image directly, I am using Intent to share image to WhatsApp.
Code is as below :
private void shareViaIntent(Context mContext, int iconPosition) {
try {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType(getResources().getString(R.string.strIntentType));
Uri uri = null;
if (mStrPackageName.equals(getResources().getString(R.string.strWhatsAppPackage))) {
//inorder to send images with white background to WhatsApp.
uri = FileProvider.getUriForFile(mContext, getResources().getString(R.string.strFileProviderPackage), mArray_wapp_Icons[iconPosition]);
} else {
uri = FileProvider.getUriForFile(mContext, getResources().getString(R.string.strFileProviderPackage), mArray_Icons[iconPosition]);
}
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setPackage(mStrPackageName);
startActivity(shareIntent);
} catch (RuntimeException runTimeException) {
runTimeException.printStackTrace();
} catch (Exception exception) {
exception.printStackTrace();
}
}
Now, The issue is that When I am in open chat window for specific person. Means In WhatsApp app, If i am chatting with person named "xyz", then image is not sending directly to person named "xyz". It opens "SEND TO" window in WhatsApp, where i can select multiple contact to share image.
.
But, I just have to share selected image to that particualr "xyz" person.
So, Is there any changes needed in Intent as I have coded ?
Please, Guide.

Android Intent share Image from url path without saving in local

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.

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.

How to send a picture via email in Android, previewed but NOT attached..?

I want to send a picture preview in my mail Intent.
I dont want to attach it, i just want it to be shown.
This is my intent:
String textToSend = getString(R.string.mailHi)+"<br><br>"+getString(R.string.mailText)+getTextToSendViaMail();
Uri pngUri = null;
File currentShopImage = new File(Environment.getExternalStorageDirectory().toString()+"/OpenGuide/"+Integer.toString(keyId)+"_1_normal.pn_");
if(currentShopImage.exists()){
File pngFile = new File(currentShopImage.toString());
pngUri = Uri.fromFile(pngFile);
}
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
//i.putExtra(Intent.EXTRA_EMAIL, new String[] { emailCim });
i.putExtra(Intent.EXTRA_SUBJECT, "OpenGuide");
i.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(textToSend));
if(pngUri!= null)
i.putExtra(Intent.EXTRA_STREAM, pngUri);
try {
startActivity(Intent.createChooser(i, getString(R.string.SendMail)));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(ShopActivity.this, getString(R.string.MailClientNotFound), Toast.LENGTH_SHORT).show();
}
How can i achive such a thing ?
As far as I understood your problem, you want to place the image inside the mail with other text. In the words of Email protocol it's called an INLINE Attachment.
You would not be able to do this with intents as none of the email clients installed on the device supports creating html messages.
If it's really a core part of your app, you should consider a third party api to do this. One of the such library is JavaMail. You would be able to send html messages through this library but will take some time in setting up.
Here's a link that may give you some hint, how it's done.
Then you need to send a HTML generated email afaik.

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