Android 呼叫系統的分享介面,進行檔案分享
阿新 • • 發佈:2018-11-28
//分享文字
public void shareText(View view) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "text內容.");
shareIntent.setType("text/plain");
//設定標題(彈出分享列表的介面標題),
startActivity(Intent.createChooser(shareIntent, "分享到"));
}
//分享一張圖片
public void shareSingleImage(View view) {
String imagePath = Environment.getExternalStorageDirectory() + File.separator + "aaa.jpg";
//由檔案得到uri
Uri imageUri = Uri.fromFile(new File(imagePath)); //imagePath--本地的檔案路徑
Log.d("share", "uri:" + imageUri);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享到:"));
}
//分享多張圖片 (儲存一個集合)
public void shareMultipleImage(View view) {
ArrayList<Uri> uriList = new ArrayList<>();
String path = Environment.getExternalStorageDirectory() + File.separator;
uriList.add(Uri.fromFile(new File(filePath+"aaa-1.jpg")));
uriList.add(Uri.fromFile(new File(filePath+"aaa-2.jpg")));
uriList.add(Uri.fromFile(new File(filePath+"aaa-3.jpg")));
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享到:"));
}
public void shareText(View view) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "text內容.");
shareIntent.setType("text/plain");
//設定標題(彈出分享列表的介面標題),
startActivity(Intent.createChooser(shareIntent, "分享到"));
}
//分享一張圖片
public void shareSingleImage(View view) {
String imagePath = Environment.getExternalStorageDirectory() + File.separator + "aaa.jpg";
//由檔案得到uri
Uri imageUri = Uri.fromFile(new File(imagePath)); //imagePath--本地的檔案路徑
Log.d("share", "uri:" + imageUri);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享到:"));
}
//分享多張圖片 (儲存一個集合)
public void shareMultipleImage(View view) {
ArrayList<Uri> uriList = new ArrayList<>();
String path = Environment.getExternalStorageDirectory() + File.separator;
uriList.add(Uri.fromFile(new File(filePath+"aaa-1.jpg")));
uriList.add(Uri.fromFile(new File(filePath+"aaa-2.jpg")));
uriList.add(Uri.fromFile(new File(filePath+"aaa-3.jpg")));
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享到:"));
}
}
//分享所有型別檔案:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(new File(filePath)));
shareIntent.setType("*/*");//此處可傳送多種檔案
startActivity(Intent.createChooser(shareIntent, “分享到:”));