2015-11-26 112 views
1

我有這種方法共享一個文本文件或圖片,取決於我使用的EXTRA_STREAM。我已經theese兩個I可以從Android共享意圖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]" }); 
    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(); 
    } 
} 
+0

好的,謝謝了。我的問題是當我打電話給startActivity時,怎樣才能同時共享這兩個文件? –

+0

我意圖的主要用途只是用電子郵件發送意圖。 –

回答

0

檢查thisthis

一個內容URI是你所需要的。 有沒有必要使用文件Uri。

在未來的其他應用程序可能希望避免讀取文件Uri所需的READ_EXTERNAL_STORAGE。所以你可以避開它們。


如果你只是想與不同類型的共享文件,使用ACTION_SEND_MULTIPLE

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

這就是我正在尋找。 –

+0

@ Waffles.Inc沒問題。我不知何故誤解了這個問題。 – FlanschiFox