使用动作发送分享图像

Share image with action send

所以我正在尝试使用 Intent.ACTION_SEND 从我的应用程序分享图片。

这是我正在使用的代码

Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, shareImgURI);
    shareIntent.setType("image/*");
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(Intent.createChooser(shareIntent, "Share images..."));

shareImgURIcontent://com.android.providers.media.documents/document/image%3A298

它打开了选择器,但是当我 select 一个选项时没有任何反应。我错过了什么吗?

使用下面的代码

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@SuppressWarnings( "deprecation" )
public static Intent shareImage(Context context, String pathToImage) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
    shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
else
    shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);

shareIntent.setType("image/*");

// For a file in shared storage.  For data in private storage, use a ContentProvider.
Uri uri = Uri.fromFile(context.getFileStreamPath(pathToImage));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
return shareIntent;}

将您的意图更改为:

 Intent shareIntent = new Intent(Intent.ACTION_SEND);
 shareIntent.setType("image/jepg");
 shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+shareImgURI));
 startActivity(Intent.createChooser(shareIntent, "Share image"));

并在您的 activity 的 onCreate() 方法中添加此代码

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());

试试这个

Bitmap icon = mBitmap;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
try {
    f.createNewFile();
    FileOutputStream fo = new FileOutputStream(f);
    fo.write(bytes.toByteArray());
} catch (IOException e) {                       
        e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));
Bitmap icon = mBitmap;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator 
+ "temporary_file.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {                       
    e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, 
Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));

试一下它会起作用。

Picasso.with(getApplicationContext()).load("image url path").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));
            context.startActivity(Intent.createChooser(i, "Share using"));
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
        }
    });




private Uri getLocalBitmapUri(Bitmap bmp) {
Uri bmpUri = null;
try {
    File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
    FileOutputStream out = new FileOutputStream(file);
    bmp.compress(Bitmap.CompressFormat.PNG, 50, out);
    out.close();
    bmpUri = Uri.fromFile(file);
} catch (IOException e) {
    e.printStackTrace();
}
  return bmpUri;
 }

一个老问题,但为未来的访问者发布。

Kotlin 分享图片的代码。

您需要提供 content//: 类型的 URI 以适应 Android N 及以上的文件共享方案。

val sharingIntent = Intent(android.content.Intent.ACTION_SEND)
        sharingIntent.type = "image/*"
        sharingIntent.putExtra(Intent.EXTRA_STREAM, uri)

        context.startActivities(arrayOf(Intent.createChooser(sharingIntent, "Share with")))

可能晚了,只是在 Whosebug 上找到了这个答案,不幸的是不记得这是谁的答案了。在 Lollipop、Marshmallow、Oreo 上测试 -> 有效。

  Uri uri = FileProvider.getUriForFile(this, "com.example.provider", new File(photoPath));
        Intent share = ShareCompat.IntentBuilder.from(this)
                .setStream(uri) // uri from FileProvider
                .setType("text/html")
                .getIntent()
                .setAction(Intent.ACTION_SEND) //Change if needed
                .setDataAndType(uri, "image/*")
                .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

startActivity(Intent.createChooser(share, getString(R.string.share_image)));