我想使用的代码共享图像:
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);
我做了一个按钮,拨打上面的代码。 份额的意图打开,但如果我点击“通过彩信分享”我:“不能将此图片添加到您的信息”。 如果Facebook我只拿到了一个文本区域,没有我的照片。
Answer 1:
@的eclass的回答改编版,它不需要使用的ImageView的:
用毕加索的url加载到一个位图
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) { }
});
}
位图转换成乌里
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;
}
Answer 2:
我使用这些代码从本教程
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
}
然后添加到您的活动
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;
}
然后添加您的应用程序清单
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Answer 3:
你需要使用一个本地文件。 像这样:
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"));
如果图像是远程服务器上首先下载到设备。
Answer 4:
大量的琢磨后,这里是我发现是为我工作的代码! 我认为这是代码最简单的版本,以实现利用毕加索给定的任务之一。 有没有需要创建一个ImageView的对象。
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
bmp = bitmap;
String path = MediaStore.Images.Media.insertImage(getContentResolver(), bmp, "SomeText", null);
Log.d("Path", path);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "Hey view/download this image");
Uri screenshotUri = Uri.parse(path);
intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share image via..."));
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
String url = "http://efdreams.com/data_images/dreams/face/face-03.jpg";
Picasso.with(getApplicationContext()).load(url).into(target);
在这里,BMP是一类级别的位图变量和URL将是动态的互联网网址到您的共享画面。 此外,而不是保持代码的onBitmapLoaded()函数内部共享,也可以保持内部的其他处理功能和后来从onBitmapLoaded()函数调用。 希望这可以帮助!
Answer 5:
试试这个:
new OmegaIntentBuilder(context)
.share()
.filesUrls("http://stacktoheap.com/images/stackoverflow.png")
.download(new DownloadCallback() {
@Override
public void onDownloaded(boolean success, @NotNull ContextIntentHandler contextIntentHandler) {
contextIntentHandler.startActivity();
}
});
https://github.com/Omega-R/OmegaIntentBuilder
Answer 6:
首先,你需要在滑行加载图像。 然后,你可以分享给任何地方。 代码从滑行加载图像(图像保存到存储,以后可以将其删除)。
Glide.with(getApplicationContext())
.load(imagelink)\\ link of your image file(url)
.asBitmap().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE)
.into(new SimpleTarget < Bitmap > (250, 250) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "Hey view/download this image");
String path = MediaStore.Images.Media.insertImage(getContentResolver(), resource, "", null);
Log.i("quoteswahttodo", "is onresoursereddy" + path);
Uri screenshotUri = Uri.parse(path);
Log.i("quoteswahttodo", "is onresoursereddy" + screenshotUri);
intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share image via..."));
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_SHORT).show();
super.onLoadFailed(e, errorDrawable);
}
@Override
public void onLoadStarted(Drawable placeholder) {
Toast.makeText(getApplicationContext(), "Starting", Toast.LENGTH_SHORT).show();
super.onLoadStarted(placeholder);
}
});
Answer 7:
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int permissionCheck = ContextCompat.checkSelfPermission(SingleProduct.this,
Manifest.permission.READ_EXTERNAL_STORAGE);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
Log.e("MainActivity ", "P granted");
bmpUri = getLocalBitmapUri(imageView);
} else {
ActivityCompat.requestPermissions(SingleProduct.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
} else {
Log.e("MainActivity", "Lower Than MarshMallow");
bmpUri = getLocalBitmapUri(imageView);
}
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.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share Image"));
} else {
Toast.makeText(SingleProduct.this, "Sharing Failed !!", Toast.LENGTH_SHORT).show();
}
}
});
public Uri getLocalBitmapUri(ImageView imageView) {
Drawable drawable = imageView.getDrawable();
Bitmap bmp = null;
if (drawable instanceof BitmapDrawable) {
bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
} else {
return null;
}
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;
}
//for oreo add below code in manifest under application tag
<provider
android:name=".utility.GenericFileProvider"
android:authorities="${applicationId}.your package
name.utility.GenericFileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
create class that extends FileProvider
public class MyFileProvider extends FileProvider {
}
for oreo add this code
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
bmpUri = FileProvider.getUriForFile(this,
this.getApplicationContext().getPackageName() +
".your package name.GenericFileProvider", file);
} else {
bmpUri = Uri.fromFile(file);
}
finally add provider_paths.xml in res/xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
就这样
Answer 8:
通过使用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"));
注册在意向
如果你想上市的应用程序时,这个意图是所谓的,那么你必须在你的manifest.xml文件添加一个意图过滤器
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
文章来源: android share image from url