我想使用代码共享图像:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri imageUri = Uri.parse("http://stacktoheap.com/images/stackoverflow.png");
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(sharingIntent);
Run Code Online (Sandbox Code Playgroud)
我做了一个调用上面代码的按钮.共享意图打开但我点击"通过彩信共享":"无法将此图片添加到您的消息".如果Facebook我只有一个没有我的照片的文本区域.
Aar*_*haw 36
@ eclass答案的改编版本,不需要使用ImageView:
使用Picasso将URL加载到Bitmap中
public void shareItem(String url) {
Picasso.with(getApplicationContext()).load(url).into(new Target() {
@Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/*");
i.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bitmap));
startActivity(Intent.createChooser(i, "Share Image"));
}
@Override public void onBitmapFailed(Drawable errorDrawable) { }
@Override public void onPrepareLoad(Drawable placeHolderDrawable) { }
});
}
Run Code Online (Sandbox Code Playgroud)
将位图转换为Uri
public Uri getLocalBitmapUri(Bitmap bmp) {
Uri bmpUri = null;
try {
File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
Run Code Online (Sandbox Code Playgroud)
小智 21
我使用本教程中的这些代码
final ImageView imgview= (ImageView)findViewById(R.id.feedImage1);
Uri bmpUri = getLocalBitmapUri(imgview);
if (bmpUri != null) {
// Construct a ShareIntent with link to image
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/*");
// Launch sharing dialog for image
startActivity(Intent.createChooser(shareIntent, "Share Image"));
} else {
// ...sharing failed, handle error
}
Run Code Online (Sandbox Code Playgroud)
然后将其添加到您的活动中
public Uri getLocalBitmapUri(ImageView imageView) {
// Extract Bitmap from ImageView drawable
Drawable drawable = imageView.getDrawable();
Bitmap bmp = null;
if (drawable instanceof BitmapDrawable){
bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
} else {
return null;
}
// Store image to default external storage directory
Uri bmpUri = null;
try {
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
file.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
Run Code Online (Sandbox Code Playgroud)
然后添加您的应用程序清单
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)
您需要使用本地文件。像这样:
Uri imageUri = Uri.parse("android.resource://your.package/drawable/fileName");
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(intent , "Share"));
Run Code Online (Sandbox Code Playgroud)
如果您的图像在远程服务器上,请先将其下载到设备上。
And*_*tic -1
通过使用 createchooser 你可以实现这一点,
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse(path);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
Run Code Online (Sandbox Code Playgroud)
注册意向
如果您希望在调用此 Intent 时列出您的应用程序,则必须在您的 manifest.xml 文件中添加一个 Intent 过滤器
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
45989 次 |
| 最近记录: |