Android Share Intent EXTRA_STREAM

*爱你&永不变心* 提交于 2019-12-11 10:36:34

问题


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

How can I share both at the same time when I call startActivity?

Here is my code

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();
    }
}

回答1:


Check this out and this out

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

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


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("*/*");


来源:https://stackoverflow.com/questions/33944944/android-share-intent-extra-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!