如何在意图“Intent.EXTRA_ALLOW_MULTIPLE”上设置最大文件选择计数。用户最多可以选择一定的限制?
Posted
技术标签:
【中文标题】如何在意图“Intent.EXTRA_ALLOW_MULTIPLE”上设置最大文件选择计数。用户最多可以选择一定的限制?【英文标题】:How to set maximum files selection count on intent "Intent.EXTRA_ALLOW_MULTIPLE".User can select upto certain limit? 【发布时间】:2018-11-30 11:15:03 【问题描述】:我使用以下 Intent 类型并使用“Intent.EXTRA_ALLOW_MULTIPLE”来选择多个文件选择,但我想将最大文件选择计数设置为 Intent,而不在 android 应用程序中使用自定义类。
Intent intent = new Intent();
intent.setType("*/*");
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, true);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
String[] extraMimeTypes = "image/*", "video/*", "application/pdf", "audio/*",
"application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.ms-powerpoint", "application/vnd.openxmlformats-officedocument.presentationml.presentation",
"application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",;
intent.putExtra(Intent.EXTRA_MIME_TYPES, extraMimeTypes);
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
startActivityForResult(intent, 201);
我想在 android Intent 中发送文件选择计数。
【问题讨论】:
你有没有找到限制文件选择的解决方案? 我也想问同样的问题。如何通过文件数限制文档选择。 【参考方案1】:我能想到的唯一方法是检查 OnActivityResult() 方法的 Intent 数据 参数中收到的文件数使用 ClipData 并在超出限制时调用 Intent。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK&&requestCode==201&&data!=null)
ClipData clip=data.getClipData();
if(clip==null)//data.getClipData() returns null if there is ONE item received
Toast.makeText(this,"You have selected only one item!",Toast.LENGTH_LONG).show();
//To receive and manage a single data because clip gets null in this case
ClipData.Item item = new ClipData.Item(data.getData());
ClipDescription description=new ClipDescription("clip",new String[1]);
clip=new ClipData(description,item);
//You have your single data in clip
else
if(clip.getItemCount()>YOUR_LIMIT)
Toast.makeText(this,"You have selected greater items than the maximum limit! Reselect your files again!",Toast.LENGTH_LONG).show();
Intent intent = new Intent();
intent.setType("*/*");
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, true);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
String[] extraMimeTypes = "image/*", "video/*", "application/pdf", "audio/*",
"application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.ms-powerpoint", "application/vnd.openxmlformats-officedocument.presentationml.presentation",
"application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",;
intent.putExtra(Intent.EXTRA_MIME_TYPES, extraMimeTypes);
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
startActivityForResult(intent, 201);
else
//Manage Data Received in ClipData clip
【讨论】:
以上是关于如何在意图“Intent.EXTRA_ALLOW_MULTIPLE”上设置最大文件选择计数。用户最多可以选择一定的限制?的主要内容,如果未能解决你的问题,请参考以下文章
如何在意图“Intent.EXTRA_ALLOW_MULTIPLE”上设置最大文件选择计数。用户最多可以选择一定的限制?
iOS SiriKit:在意图“解决”和“处理”方法之间传递数据
重新启动后的警报管理器在意图时显示错误 NullPointerException