Android 分享操作(1)------分享資料

來源:互聯網
上載者:User

標籤:art   action   arraylist   類型   不同類   jpeg   列表   ring   div   

一.分享資料分享文本資料:

  ACTION_SEND最直接常用的地方是從一個Activity發送常值內容到另外一個Activity。例如,Android內建的瀏覽器可以將當前顯示頁面的URL作為常值內容分享到其他程式。這一功能對於通過郵件或者社交網路來分享文章或者網址給好友而言是非常有用的。

Intent sendIntent = new Intent();sendIntent.setAction(Intent.ACTION_SEND);sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");sendIntent.setType("text/plain");startActivity(sendIntent);

  如果為intent調用了Intent.createChooser(),那麼Android總是會顯示可供選擇。這樣有一些好處:

  • 即使使用者之前為這個intent設定了預設的action,選擇介面還是會被顯示。
  • 如果沒有匹配的程式,Android會顯示系統資訊。
  • 我們可以指定選擇介面的標題。
Intent sendIntent = new Intent();sendIntent.setAction(Intent.ACTION_SEND);sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");sendIntent.setType("text/plain");startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to));

  如下:
  

分享二進位檔案

分享二進位檔案(片)也是類似的:分享二進位的資料需要結合設定特定的MIME類型,需要在EXTRA_STREAM`裡面放置資料的URI,下面有個分享圖片的例子,該例子也可以修改用於分享任何類型的位元據:

Intent sendIntent = new Intent();sendIntent.setAction(Intent.ACTION_SEND);sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");sendIntent.setType("text/plain");startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to));
發送多塊內容(Send Multiple Pieces of Content)

  為了同時分享多種不同類型的內容,需要使用ACTION_SEND_MULTIPLE與指定到那些資料的URIs列表。MIME類型會根據分享的混合內容而不同。例如,如果分享3張JPEG的圖片,那麼MIME類型仍然是image/jpeg。如果是不同圖片格式的話,應該是用image/*來匹配那些可以接收任何圖片類型的activity。如果需要分享多種不同類型的資料,可以使用*/*來表示MIME。像前面描述的那樣,這取決於那些接收的程式解析並處理我們的資料。需要保證有URL的存取權限。

ArrayList<Uri> imageUris = new ArrayList<Uri>();imageUris.add(imageUri1); // Add your image URIs hereimageUris.add(imageUri2);Intent shareIntent = new Intent();shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);shareIntent.setType("image/*");startActivity(Intent.createChooser(shareIntent, "Share images to..")); 
二.接收資料1.更新我們的manifest檔案

  主要是設定intent fliters來告訴系統願意接收那些資料。

2.處理接受到的資料

  資料通過Intent傳輸,所以通過調用getIntent()方法來擷取到Intent對象。拿到這個對象後,我們可以對其中面的資料進行判斷,從而決定下一步行為。

void onCreate (Bundle savedInstanceState) {    ...    // Get intent, action and MIME type    Intent intent = getIntent();    String action = intent.getAction();    String type = intent.getType();    if (Intent.ACTION_SEND.equals(action) && type != null) {        if ("text/plain".equals(type)) {            // Handle text being sent        } else if (type.startsWith("image/")) {            // Handle single image being sent        }    } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {        if (type.startsWith("image/")) {            // Handle multiple images being sent        }    } else {        // Handle other intents, such as being started from the home screen    }    ...} 

 

 

 

 

 

Android 分享操作(1)------分享資料

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.