How to share / attach image from server in gmail in android

I have an image gallery where images come from the server. I want to share / attach an image in gmail. I use "Adding a simple action". http://developer.android.com/training/sharing/shareaction.html#set-share-intent

At first I tried to share the image with my SDCard, and I was able to do this using the code below.

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); sharingIntent.setType("image/jpeg"); String shareBody = "Here is the share content body"; sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject"); sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody); sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/DCIM/Camera/20130503_133024.jpg")); mShareActionProvider.setShareIntent(sharingIntent); 

When I tried to transfer the URL of my server using the code below, then when I sent the letter, I received a message that "it is impossible to attach the image."

Uri.parse (" http://lh6.googleusercontent.com/-jZgveEqb6pg/T3R4kXScycI/AAAAAAAAAE0/xQ7CvpfXDzc/s1024/sample_image_01.jpg ")

Please help me share the image from the server.

+4
source share
2 answers

It seems that the intent types STREAM and EXTRA_STREAM are not well defined and ultimately depend on how the target application interprets them. If you want the image to be binary, to be in the letter, a safer way is to download the image from the server, attach it to the intention yourself. Here is more light on the topic: "android.intent.extra.STREAM"

+1
source

After a lot of time, finally I found a solution:

  URL url = null; try { url = new URL(imageurl); } catch (MalformedURLException e) { e.printStackTrace(); } HttpURLConnection connection = null; InputStream input = null; try { connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); input = connection.getInputStream(); } catch (IOException e) { e.printStackTrace(); } Bitmap immutableBpm = BitmapFactory.decodeStream(input); Bitmap mutableBitmap = immutableBpm.copy(Bitmap.Config.ARGB_8888, true); View view = new View(this); view.draw(new Canvas(mutableBitmap)); String path = Images.Media.insertImage(getContentResolver(), mutableBitmap, "rbt", null); Uri uri = Uri.parse(path); Intent intent = new Intent(); intent.setType("image/*"); intent.putExtra(Intent.EXTRA_STREAM, uri); intent.putExtra(Intent.EXTRA_EMAIL, new String[]{" test.android@gmail.com "}); intent.putExtra(Intent.EXTRA_SUBJECT, subject); intent.putExtra(Intent.EXTRA_TEXT, body); intent.setPackage("com.google.android.gm"); startActivity(intent); 

And add below permission in manifest.xml

  <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

It works perfect for me.

0
source

Source: https://habr.com/ru/post/1487648/


All Articles