1.activity_main.xml
建立一個分享按鈕
2.MainActivity.java
private Button shareButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shareButton = (Button) findViewById(R.id.shareButton);
shareButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
shareDialog();
}
});
}
// 分享app
void shareDialog() {
String shareText = "與你分享的快樂勝過獨自擁有"; //
//Uri imageUri = Uri.parse("android.resource://" + getPackageName() + "/drawable/" + "ic_launcher"); //分享圖片至gmail、twitter可,line、facebook不行
//Log.i("imageUri:", imageUri + "");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain"); //文字檔類型
shareIntent.putExtra(Intent.EXTRA_TEXT, shareText); //傳送文字
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//shareIntent.setType("image/png"); //圖片png檔類型
// shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri); //傳送圖片
startActivity(Intent.createChooser(shareIntent, "分享"));
}
參考來源:http://hscc.cs.nctu.edu.tw/~lincyu/Android/ShareText.pdf
http://stackoverflow.com/questions/20333186/how-to-share-imagetext-using-action-send-in-android
沒有留言 :
張貼留言