Android分享意向EXTRA_STREAM [英] Android Share Intent EXTRA_STREAM

查看:880
本文介绍了Android分享意向EXTRA_STREAM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有根据我使用的EXTRA_STREAM共享文本文件或图片的方法.我有两个,我可以选择

I have this method that shares a textfile or a picture depending of wich EXTRA_STREAM I'm using. I have theese two i can choose from

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

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

How can I share both at the same time when I call 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[] { "ttj@live.se" });
    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();
    }
}

推荐答案

检查退出

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

A content Uri is all you need. There is no need to use a file Uri.

将来,其他应用可能要避免读取File Uri所需的READ_EXTERNAL_STORAGE.因此,您可以避免使用它们.

In future other apps may want to avoid READ_EXTERNAL_STORAGE which is required to read File Uri's. So you may avoid them.

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

If you just want to share files with different type, use ACTION_SEND_MULTIPLE

intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, yourUriArrayList);
intent.setType("*/*");

这篇关于Android分享意向EXTRA_STREAM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆