public FileHandler(Context context, DeviceScreen screen) { logger = UpmobileExceptionReporter.getInstance(context); databaseHelper = DatabaseHelper.getInstance(context); files = databaseHelper.getPictures(); width = screen.getSmallestWidth(); height = screen.getLargestWidth(); comparator = PictureData.TIME_COMPARATOR; Collections.sort(files, comparator); }
private synchronized PictureData addFile(File file, String from) { cleanup(file.length()); try { Bitmap bmp = decodeFile(file); if (null == bmp) return null; // not an image Bitmap scaled = scaleAndRotate(bmp, file); String path = new File(SkylightApp.getPicturesDir(), "pic" + System.currentTimeMillis() + ".jpg") .getAbsolutePath(); OutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(path)); scaled.compress(CompressFormat.JPEG, 85, out); } catch (IOException e) { Log.e(getClass().getName(), "Error writing scaled bitmap", e); logger.logException(e); } finally { if (out != null) { try { out.flush(); } catch (Exception e) { Log.e("Flushing OutputStream", e.getMessage(), e); } try { out.close(); } catch (Exception e) { Log.e("Closing OutputStream", e.getMessage(), e); } out = null; } } locked = true; PictureData picture = new PictureData(path, from); picture = databaseHelper.addOrUpdate(picture); file.delete(); scaled.recycle(); bmp.recycle(); locked = false; return picture; } catch (IllegalStateException e) { Log.e("Adding file", e.getMessage(), e); locked = false; return null; // PushLink.sendAsyncException(e); } }