这里是我的代码,但是这是一个单一的文件解决方案。
我可以共享多个文件和上传就像我下面单个文件吗?
Button btn = (Button)findViewById(R.id.hello);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/pic.png";
File file = new File(path);
MimeTypeMap type = MimeTypeMap.getSingleton();
intent.setType(type.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(path)));
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.putExtra(Intent.EXTRA_TEXT, "1111");
startActivity(intent);
}
});
是的,但你需要使用Intent.ACTION_SEND_MULTIPLE
而不是Intent.ACTION_SEND
。
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra(Intent.EXTRA_SUBJECT, "Here are some files.");
intent.setType("image/jpeg"); /* This example is sharing jpeg images. */
ArrayList<Uri> files = new ArrayList<Uri>();
for(String path : filesToSend /* List of the files you want to send */) {
File file = new File(path);
Uri uri = Uri.fromFile(file);
files.add(uri);
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);
startActivity(intent);
这绝对可以简化,但我留在,所以你可以分解所需要的每一步一些行。
UPDATE:开始在API 24,共享文件的URI会引起FileUriExposedException。 为了解决这个问题,你可以你的compileSdkVersion切换到23以下,或者您可以使用内容的URI与FileProvider 。
更新(更新): 谷歌近日宣布新的应用程序和应用程序更新将被要求指定Android的发布到Play商店的最新版本之一。 这就是说,针对API 23或更低的不再是一个有效的选择,如果你打算将应用发布到店。 你必须去的FileProvider路线。
下面是MCeley的解决方案的简易小改进版本。 这可以被用来发送异构文件列表(例如图像,文档,并在同一时间视频),比如上传下载的文件,在同一时间的图像。
public static void shareMultiple(List<File> files, Context context){
ArrayList<Uri> uris = new ArrayList<>();
for(File file: files){
uris.add(Uri.fromFile(file));
}
final Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("*/*");
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
context.startActivity(Intent.createChooser(intent, context.getString(R.string.ids_msg_share)));
}
/*
manifest file outside the applicationTag write these permissions
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> */
File pictures = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
//Get a top-level public external storage directory for placing files of a particular type.
// This is where the user will typically place and manage their own files,
// so you should be careful about what you put here to ensure you don't
// erase their files or get in the way of their own organization...
// pulled from Standard directory in which to place pictures that are available to the user to the File object
String[] listOfPictures = pictures.list();
//Returns an array of strings with the file names in the directory represented by this file. The result is null if this file is not a directory.
Uri uri=null;
ArrayList<Uri> arrayList = new ArrayList<>();
if (listOfPictures!=null) {
for (String name : listOfPictures) {
uri = Uri.parse("file://" + pictures.toString() + "/" + name );
arrayList.add(uri);
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra(Intent.EXTRA_STREAM, arrayList);
//A content: URI holding a stream of data associated with the Intent, used with ACTION_SEND to supply the data being sent.
intent.setType("image/*"); //any kind of images can support.
chooser = Intent.createChooser(intent, "Send Multiple Images");//choosers title
startActivity(chooser);
}