gpt4 book ai didi

android - 如何设置 Intent "Intent.EXTRA_ALLOW_MULTIPLE"的最大文件选择计数。用户最多可以选择一定的限制?

转载 作者:行者123 更新时间:2023-12-02 13:31:48 33 4
gpt4 key购买 nike

我使用以下 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 中发送文件选择计数。

最佳答案

我能想到的唯一方法是检查 OnActivityResult() 方法中的 Intent data 参数内收到的文件数量使用 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
}
}
}
}

关于android - 如何设置 Intent "Intent.EXTRA_ALLOW_MULTIPLE"的最大文件选择计数。用户最多可以选择一定的限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50963156/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com