1

所以我试图用来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

它会打开选​​择器,但是当我选择一个选项时,什么也没有发生。我错过了什么吗?

4

7 回答 7

6

尝试这个

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"));
于 2017-10-25T13:12:54.217 回答
4

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

共享图像的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")))
于 2019-03-14T12:06:03.283 回答
1

将您的意图更改为:

 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"));

并将此代码添加到您的活动onCreate()方法中

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
于 2017-10-25T12:59:47.627 回答
1

可能已经晚了,但只是在 StackoverFlow 上找到这个答案,不幸的是不记得是谁的答案。在棒棒糖、棉花糖、奥利奥上测试 -> 工作。

  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)));    
于 2019-05-19T17:41:33.660 回答
0

使用下面的代码

@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;}
于 2017-10-25T12:54:59.343 回答
0
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;
 }
于 2019-03-04T11:02:44.647 回答
0
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"));

试试这个它会工作。

于 2017-10-27T23:00:51.770 回答