如何在android中共享/附加gmail服务器上的图像
我有一个图像库,图像来自服务器。我想在gmail中共享/附加图像。我正在使用“添加一个简单的共享操作”。 最初,我尝试从SD卡共享图像,我可以使用下面的代码来实现如何在android中共享/附加gmail服务器上的图像,android,Android,我有一个图像库,图像来自服务器。我想在gmail中共享/附加图像。我正在使用“添加一个简单的共享操作”。 最初,我尝试从SD卡共享图像,我可以使用下面的代码来实现 Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); sharingIntent.setType("image/jpeg"); String shareBody = "Here is the share c
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);
当我试图通过使用下面的代码传递我的服务器图像url时,在发送电子邮件时,我收到一条消息说“无法附加图像”
parse(“”)
请帮助我共享服务器上的图像。流和额外流的意图类型似乎没有得到很好的定义,最终取决于目标应用程序如何解释它们。 如果您想确保图像作为二进制文件存在于电子邮件中,更安全的方法是从服务器下载图像,然后自己将其附加到邮件中。
这里有更多关于这个主题的信息:在花了很多时间之后,我终于找到了解决方案:
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);
并在manifest.xml中添加以下权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
它对我来说非常适合