这是我的代码,但这是针对单文件解决方案的。
我可以像下面的单个文件一样共享多个文件和上传吗?
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);
这绝对可以简化,但我留下了一些行,以便您可以分解所需的每个步骤。
更新:从 API 24 开始,共享文件 URI 将导致 FileUriExposedException。要解决这个问题,您可以将 compileSdkVersion 切换到 23 或更低,或者您可以使用 content URIs with a FileProvider .
更新(更新):Google recently announced新的应用程序和应用程序更新将需要针对最新版本的 Android 之一发布到 Play 商店。也就是说,如果您计划将应用程序发布到商店,则以 API 23 或更低版本为目标不再是一个有效的选择。你必须走 FileProvider 路线。
关于java - 如何通过 Intent 共享多个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15577438/