execute alertDialog when folder item is 20

时间:2016-12-09 12:54:47

标签: java android mobile

When activity is running and the user capturing an image, I want to count at the same time and check the number of files in image directory once the count reach 20 it will prompt the user if execute the task.

am thinking doing this to put this on my onCreate method

the code likes this:

File dir = new File(FilePath);
File[] file = dir.listfiles();
for(int i = 0; i < 20; i++){
      if(files[i] == 20){
          alertDialog();
    }else{
       button.setEnable(true);
    }
}

How can I achieve this on android and where should I put this thing to work on runtime?

1 个答案:

答案 0 :(得分:1)

To count the number of files in the directory, you could just simply check the length of your array, no need to iterate over it.

If you would like this code to run after the user captured an image, putting it in onCreate() does not make much sense.

According to your code in the comment below, you should perform this operation in onCaptureCompleted(), when the capture is actually finished (and the image is saved):

@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                               @NonNull CaptureRequest request,
                               @NonNull TotalCaptureResult result) {
    process(result);

    File dir = new File(filePath);
    File[] files = dir.listFiles();

    if (files.length >= 20){
        // ...
    } else{
        // ...
    }
}
相关问题