本博客講述的是:
自定義應用分享文本,圖片,多個圖片到本身的具體實現(本文中的ShareActionProvider未使用成功,所以已經註釋掉了)
代碼下載地址-日後補充(因公司禁權)
代碼(全):
1.activity_main.xml
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:text="這是一個簡單的分享程序,歡迎嘗試使用~"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_sharetext"
android:text="share text/plain"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_sharetextimprove"
android:text="share text/plain--improve"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_shareimage"
android:text="share image"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_shareimages"
android:text="share images"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
2.MainActivity.java
package com.example.chenl_fnst.share;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ShareActionProvider;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity implements View.OnClickListener{
private ShareActionProvider mShareActionProvider;
private Button btn_sharetext;
private Button btn_sharetextimprove;
private Button btn_shareimage;
private Button btn_shareimages;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_sharetext = (Button) findViewById(R.id.btn_sharetext);
btn_sharetextimprove = (Button) findViewById(R.id.btn_sharetextimprove);
btn_shareimage = (Button) findViewById(R.id.btn_shareimage);
btn_shareimages = (Button) findViewById(R.id.btn_shareimages);
btn_sharetext.setOnClickListener(this);
btn_sharetextimprove.setOnClickListener(this);
btn_shareimage.setOnClickListener(this);
btn_shareimages.setOnClickListener(this);
//init();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.share_menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
//找到分享菜單的支持類對象
return true;
}
// //這裏來設置share的數據,設置後share菜單將可用
// public void setShareIntent(Intent shareIntent) {
// if (mShareActionProvider != null) {
// mShareActionProvider.setShareIntent(shareIntent);
// }
// }
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.menu_item_share) {
Log.d("fnst","share");
//mShareActionProvider = (ShareActionProvider) item.getActionProvider();
Toast.makeText(MainActivity.this,"share",Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
switch (v.getId())
{
//比較散亂的列出了所有的可以分享的應用
case R.id.btn_sharetext: {
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);
break;
}
//分享選擇列表,並且修改了分享列表的標題
case R.id.btn_sharetextimprove: {
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)));
break;
}
//分享圖片(本地圖片),未測試成功,因爲圖片不存在
case R.id.btn_shareimage: {
//Toast.makeText(MainActivity.this,getPath,Toast.LENGTH_SHORT).show(); // 輸出/storage/emulated/0
//Toast.makeText(MainActivity.this,AbsolutePath,Toast.LENGTH_SHORT).show(); // 輸出/storage/emulated/0
//Toast.makeText(MainActivity.this,Name,Toast.LENGTH_SHORT).show(); // 輸出0
//Toast.makeText(MainActivity.this,Parent,Toast.LENGTH_SHORT).show(); // 輸出/storage/emulated
String imagePath = Environment.getExternalStorageDirectory() + File.separator + "ESFileExplorer.jpg";
//由文件得到uri
Uri imageUri = Uri.fromFile(new File(imagePath));
Log.d("share", "uri:" + imageUri); //輸出:file:///storage/emulated/0/test.jpg
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
break;
}
//分享多張圖片(本地圖片) 失敗,沒有這個本地文件
case R.id.btn_shareimages: {
ArrayList uriimages = new ArrayList<>();
String path = Environment.getExternalStorageDirectory() + File.separator;
uriimages.add(Uri.fromFile(new File(path+"australia_1.jpg")));
uriimages.add(Uri.fromFile(new File(path+"australia_2.jpg")));
uriimages.add(Uri.fromFile(new File(path+"australia_3.jpg")));
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriimages);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享到"));
}
}
}
public void init(){
// Intent intent = getIntent();
// String action = intent.getAction();
// String type = intent.getType();
//
// if (Intent.ACTION_SEND.equals(action) && type != null) {
// if ("text/plain".equals(type)) {
// handleSendText(intent); // Handle text being sent
// } else if (type.startsWith("image/")) {
// handleSendImage(intent); // Handle single image being sent
// }
// } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
// if (type.startsWith("image/")) {
// handleSendMultipleImages(intent); // Handle multiple images being sent
// }
// } else {
// // Handle other intents, such as being started from the home screen
// }
// }
// void handleSendText(Intent intent) {
// String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
// String sharedTitle = intent.getStringExtra(Intent.EXTRA_TITLE);
// if (sharedText != null) {
// // Update UI to reflect text being shared
// Toast.makeText(MainActivity.this,sharedText,Toast.LENGTH_SHORT).show();
// }
// }
//
// void handleSendImage(Intent intent) {
// Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
// if (imageUri != null) {
// //在處理二進制數據時,應當在一另外一個線程中去處理
// // Update UI to reflect image being shared
// Toast.makeText(MainActivity.this,imageUri.toString(),Toast.LENGTH_SHORT).show();
// }
// }
//
// void handleSendMultipleImages(Intent intent) {
// ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
// if (imageUris != null) {
// //在處理二進制數據時,應當在一另外一個線程中去處理
// // Update UI to reflect multiple images being shared
// Toast.makeText(MainActivity.this,imageUris.get(0).toString(),Toast.LENGTH_SHORT).show();
// }
// }
}
3.若是想要實現分享列表中有自建應用,那麼~
(1)首先更新AndroidManifest.xml
在<activity ***></activity>中加上:
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
(2)其實加了上面一段後,其實已經可以做到分享到本應用,但爲了讓表現直觀以及瞭解簡單的分享處理,增加如下代碼:
a.MainActivity.java中:
public void init(){
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent
} else if (type.startsWith("image/")) {
handleSendImage(intent); // Handle single image being sent
}
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
if (type.startsWith("image/")) {
handleSendMultipleImages(intent); // Handle multiple images being sent
}
} else {
// Handle other intents, such as being started from the home screen
}
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
String sharedTitle = intent.getStringExtra(Intent.EXTRA_TITLE);
if (sharedText != null) {
// Update UI to reflect text being shared
Toast.makeText(MainActivity.this,sharedText,Toast.LENGTH_SHORT).show();
}
}
void handleSendImage(Intent intent) {
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
//在處理二進制數據時,應當在一另外一個線程中去處理
// Update UI to reflect image being shared
Toast.makeText(MainActivity.this,imageUri.toString(),Toast.LENGTH_SHORT).show();
}
}
void handleSendMultipleImages(Intent intent) {
ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (imageUris != null) {
//在處理二進制數據時,應當在一另外一個線程中去處理
// Update UI to reflect multiple images being shared
Toast.makeText(MainActivity.this,imageUris.get(0).toString(),Toast.LENGTH_SHORT).show();
}
}
b.MainActivity的onCreate()方法中增加init();
4.運行,你會有發現~
作爲日誌,方便以後查看~