Android 共享意图 EXTRA_STREAM

2024-02-26

我有这个方法可以共享文本文件或图片,具体取决于我使用的 EXTRA_STREAM 。我有这两个可以选择

i.putExtra(Intent.EXTRA_STREAM, uri);
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));

当我调用startActivity时如何同时共享两者?

这是我的代码

public void shareTextAndPic(){
    
    long x = getBundle();

    Product product = db.findProductbyId(getBundle());
    

    Bitmap icon = BitmapFactory.decodeByteArray(db.fetchSingle(x), 0,
            db.fetchSingle(x).length);
    
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/jpeg");

    ContentValues values = new ContentValues();
    values.put(Images.Media.TITLE, "title");
    values.put(Images.Media.MIME_TYPE, "image/jpeg");
    Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,
            values);


    OutputStream outstream;
    try {
        outstream = getContentResolver().openOutputStream(uri);
        icon.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
        outstream.close();
    } catch (Exception e) {
        System.err.println(e.toString());
    }

    //share.putExtra(Intent.EXTRA_STREAM, uri);
    //startActivity(Intent.createChooser(share, "Share Image"));
    
    
    File file = new File(way + "/momsfil.txt");
    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("text/plain");
    i.setType("image/jpeg");
    i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
    i.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected] /cdn-cgi/l/email-protection" });
    i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
    i.putExtra(Intent.EXTRA_TEXT, "body of email");
    
    
    //i.putExtra(Intent.EXTRA_STREAM, uri);
    
    try {
        startActivity(Intent.createChooser(i, "Share"));
    } catch (android.content.ActivityNotFoundException e) {
        Toast.makeText(TableRow.this,
                "There are no email clients installed.",
                Toast.LENGTH_SHORT).show();
    }
}

Check this https://commonsware.com/blog/2015/10/07/runtime-permissions-files-action-send.html出和this https://commonsware.com/blog/2014/07/04/uri-not-necessarily-file.html out

您只需要一个内容 Uri。 无需使用文件 Uri。

将来其他应用程序可能希望避免读取文件 Uri 所需的 READ_EXTERNAL_STORAGE。所以你可以避开它们。


如果您只想共享不同类型的文件,请使用ACTION_SEND_MULTIPLE

intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, yourUriArrayList);
intent.setType("*/*");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 共享意图 EXTRA_STREAM 的相关文章

随机推荐