我的应用程序中有一个文件上传模块,点击按钮,选择并上传外部存储目录(Google Drive)中的文件。根据要求,我可以选择最多5个文件,这可以通过一次选择5个文件或分割为2个文件然后再选择3个文件来完成。我只想在选择了5个以上的文件后通知用户。目前,我可以在调用onActivityResult()后通知用户。有什么办法可以将选择限制在5个文件中并通过Google云端硬盘自行通知用户吗?
请在下面找到我目前正在运行的代码:
txtv_attach.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
int_permission_type = 1;
if(Build.VERSION.SDK_INT >= 23) {
if (CheckingPermissionIsEnabledOrNot()) {
Toast.makeText(myContext, "All Permissions Granted Successfully", Toast.LENGTH_LONG).show();
start_attachment_activity();
}
// If, If permission is not enabled then else condition will execute.
else {
//Calling method to enable permission.
RequestMultiplePermission();
}
}
else
{
start_attachment_activity();
}
}
});
现在是start_attachment_activity():
public void start_attachment_activity()
{
String[] mimeTypes =
{"image/*","application/pdf","application/msword","application/vnd.ms-powerpoint","application/vnd.ms-excel","text/plain","audio/", "video/"};
Uri uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setDataAndType(uri, "resource/folder");
chooseFile.putExtra(Intent.EXTRA_STREAM, uri);
chooseFile.addCategory(Intent.CATEGORY_OPENABLE);
chooseFile.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
chooseFile.setType("*/*");
chooseFile.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
chooseFile = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult(chooseFile, PICKFILE_RESULT_CODE);
}
现在onActivityResult():
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case PICKFILE_RESULT_CODE:
if(resultCode==RESULT_OK){
if (data.getData() != null) {
if((alst_attachmentNames.size()+1)>5)
{
Toast.makeText(myContext,"attachment of 5 or less than 5 items are allowed", Toast.LENGTH_SHORT).show();
}
else
{
Uri dataURI = data.getData();
try {
String path= getFilePath(getActivity(), dataURI);
if(path!=null)
{
alst_attachmentPaths.add(path);
alst_attachmentNames.add(path.substring(path.lastIndexOf("/")+1));
LinearLayoutManager layoutManager=
new LinearLayoutManager(myContext, LinearLayoutManager.HORIZONTAL, false);
RecyclerView.Adapter attachmentAdapter = new attachmentAdapter(myContext, alst_attachmentNames);
rvw_attachmentList.setLayoutManager(layoutManager);
rvw_attachmentList.setAdapter(attachmentAdapter);
}
} catch (URISyntaxException e) {
String err = (e.getMessage()==null)?"SD Card failed":e.getMessage();
Log.e("sdcard-err2:",err);
}
}
}
if (data != null) {
ClipData clipData = data.getClipData();
if (clipData != null) {
if(clipData.getItemCount()>5 || (clipData.getItemCount()+alst_attachmentNames.size())>5 )
{
Toast.makeText(myContext,"attachment of 5 or less than 5 items are allowed", Toast.LENGTH_SHORT).show();
}
else
{
for (int i = 0; i < clipData.getItemCount(); i++) {
ClipData.Item item = clipData.getItemAt(i);
Uri uri = item.getUri();
//In case you need image's absolute path
try {
String path= getFilePath(getActivity(), uri);
alst_attachmentPaths.add(path);
alst_attachmentNames.add(path.substring(path.lastIndexOf("/")+1));
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
LinearLayoutManager layoutManager=
new LinearLayoutManager(myContext, LinearLayoutManager.HORIZONTAL, false);
RecyclerView.Adapter attachmentAdapter = new attachmentAdapter(myContext, alst_attachmentNames);
rvw_attachmentList.setLayoutManager(layoutManager);
rvw_attachmentList.setAdapter(attachmentAdapter);
}
}
}
}
break;
}
}
如果我遗失了什么,请告诉我。我想控制驱动器本身的文件选择数量。需要你的帮助。提前谢谢。