FileObserver:未处理的异常

时间:2018-10-11 16:43:09

标签: java android fileobserver

FileObserver在大多数情况下都能正常工作,但有时会发生以下异常:

FileObserver: Unhandled exception occurs

将新文件添加到文件夹时,FileObserver会观察到。但是,每次将新文件添加到文件夹时,不会发生此错误,而是随机发生,并且我不知道如何解决它。我已将代码附加到FileObserver和使用它的方法上。

(CallLatestReceiptData)仅调用另一种方法,该方法使用文件夹中新添加文件的文件路径调用addNewReceipt方法。这是代码:

private void initializeFileObserver() {
    observer = new FileObserver(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + "/" + "Receipt Manager" + "/") {
        @Override
        public void onEvent(int event, String file) {
            switch (event) {
                case FileObserver.CREATE:
                    callLatestReceiptData(file);
                    Log.d(OBSERVER_LOG, "File created 1 [" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + file + "]");
                    break;
            }
        }
    };
    observer.startWatching();
}

private void addNewReceipt(String path) {

    Log.d(RECEIPT_SERVICE_LOG, "Called Database AddNewReceipt");
    String picturePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + "/" + "Receipt Manager" + "/" + path;
    Log.d(RECEIPT_SERVICE_LOG, "Set PicturePath AddNewReceipt");
    Log.d(RECEIPT_SERVICE_LOG, picturePath);

    Bitmap rotatedBitmap = createRotateAndScaleBitmap(picturePath);
    Log.d(RECEIPT_SERVICE_LOG, "Rotated Bitmap AddNewReceipt");
    byte[] byteArrayPicture = convertBitmapToByteArray(rotatedBitmap);
    Log.d(RECEIPT_SERVICE_LOG, "Rotated Bitmap AddNewReceipt");

    String date = picturePath.replace(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + "/" + "Receipt Manager" + "/", "").replace(".jpg", "");
    Log.d(RECEIPT_SERVICE_LOG, "Set Date AddNewReceipt");
    final Receipts receipts = new Receipts("", "", date, "", "", 0, byteArrayPicture, 1);
    Log.d(RECEIPT_SERVICE_LOG, "Created New Receipt AddNewReceipt");

    receiptDatabase.daoAccessReceipt().insertOnlySingleReceipt(receipts);
    Log.d(RECEIPT_SERVICE_LOG, "Inserted In Database AddNewReceipt");
    fetchReceiptData(receipts);
    Log.d(RECEIPT_SERVICE_LOG, "Fetch Data To List AddNewReceipt");

    isDone = true;
    Log.d(RECEIPT_SERVICE_LOG, "ISDONE IS TRUE - ADD_NEW_RECEIPT");
}

Error Stack Trace

私有位图createRotateAndScaleBitmap(String filePath){

    Matrix matrix = new Matrix();
    Log.d(RECEIPT_SERVICE_LOG, "Matrix Called");
    matrix.postRotate(90);
    Log.d(RECEIPT_SERVICE_LOG, "Matrix Rotated");
    Bitmap receiptBitmap = BitmapFactory.decodeFile(filePath);
    Log.d(RECEIPT_SERVICE_LOG, "File Decoded");
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(receiptBitmap, 1280, 960, false);
    Log.d(RECEIPT_SERVICE_LOG, "Bitmap Scaled");
    Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, false);
    Log.d(RECEIPT_SERVICE_LOG, "Bitmap Rotated");

    return rotatedBitmap;
}

0 个答案:

没有答案