-7

My app record camera preview every 5 minute. record file is save on /sdcard/Movies/*.mp4

before create record file. check current storage space

//execute every 5 minute.
if (current space < MAX_SIZE) { //record start
      //record start
} else { //record impossible. try old file delete.
     RecordFile recFile = new RecordFile();
     boolean ret = recFile.deleteOldFile();
}

my situation current record impossible. try old file delete. but occur StringIndexOutOfBoundsException

my source

RecordFile.class

    private ArrayList<String> mRecFiles;
    private ArrayList<Long> mRecFilesNum;
    private HashMap<Long, String> fileMap;
    private String filePath;
    private String delFileName;

    public boolean deleteOldFile() {
          String delFname = getRecordFileList();
          File delFile = new File(filePath + delFname);
          if (delFile.delete()) {
                 //success old file delete
                 return true;
          } else {
                //fail delete
                return true;
          }
    }

    public String getRecordFileList() {
          filePath = Environment.getExternalStorageDirectory() + File.separator
                + Environment.DIRECTORY_MOVIES + File.separator;

        mRecFiles = new ArrayList();
        mRecFilesNum = new ArrayList();

        fileMap = new HashMap();

        File file = new File(filePath);
        File[] list = file.listFiles();

        for (int i = 0; i < list.length; i++) {
            String fname = list[i].getName();

            int idx = fname.lastIndexOf(".");
            String fname2 = fname.substring(0, idx);   //occur stringIndexOutOfBoundsException

            long tmp = Long.parseLong(fname2.replaceAll("[^0-9]", ""));

            fileMap.put(tmp, fname);
            mRecFilesNum.add(tmp);
        }

        long max = Collections.max(mRecFilesNum);
        long min = Collections.min(mRecFilesNum);
        Iterator<Long> iterator = fileMap.keySet().iterator();
        while (iterator.hasNext()) {
            Long key = iterator.next();

            if (key == min) {
                delFileName = fileMap.get(key);

                break;
            }
        }

        return delFileName;
    }
}

ERROR:

java.lang.StringIndexOutOfBoundsException: length=10; regionStart=0; regionLength=-1
          at java.lang.String.startEndAndLength(String.java:298)
          at java.lang.String.substring(String.java:1087)
          at kr.co.iosystem.blackeyeonandroid.record.RecordFile.getRecordFileList(RecordFile.java:72)
          at kr.co.iosystem.blackeyeonandroid.record.RecordFile.deleteBeforeFile(RecordFile.java:35)
          at kr.co.iosystem.blackeyeonandroid.BlackEyeActivity$mRecTimerHandler.handleMessage(BlackEyeActivity.java:582)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:148)
          at android.app.ActivityThread.main(ActivityThread.java:5415)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)

How to fix this problem? thanks.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
hyunwook cho
  • 85
  • 1
  • 10
  • 3
    so you have a file that doesn't contain '.' character. – mihail Nov 02 '17 at 08:46
  • Use a debugger to find out what is happening. – Jens Nov 02 '17 at 08:48
  • 3
    day after day I think people become more and more lazy... Why don't you just debug your code or put some logging. Every 3rd question on SO is now pure laziness. – mihail Nov 02 '17 at 08:48
  • 1
    I am quite surprised, not at the laziness of the question, I'm used to that, but at the fact that there are not 5 answers begging for easy points since the solution here is clear to ANYONE with a brain. – Vucko Nov 02 '17 at 08:51
  • Error can be fixed with [this link](https://stackoverflow.com/questions/8393849/how-to-get-name-of-file-object-without-its-extension-in-java) – Ellisan Nov 02 '17 at 09:27

1 Answers1

0

For unfound string lastIndexOf() method returns -1.

Add check for the idx, in your case StringIndexOutOfBoundsException occurs because the beginIndex is larger than endIndex.

dbog
  • 106
  • 1
  • 4