@Override public View getView(final int position, View convertView, ViewGroup parent) { final ImageView view = new ImageView(context); final String path = fileHandler.getFiles().get(position).getPath(); Bitmap bitmap = getBitmapFromMemCache(path); if (null == bitmap) { view.setVisibility(view.INVISIBLE); service.submit( new Runnable() { @Override public void run() { Bitmap bitmap = resizeToSmall(BitmapFactory.decodeFile(path)); addBitmapToMemoryCache(path, bitmap); setBitmap(view, bitmap); } }); } else { view.setImageBitmap(bitmap); } int sizeWidth = SkylightApp.getScreen().getWidth(); sizeWidth = (sizeWidth - horizontalSpacing * 4) / 3; view.setLayoutParams(new GridView.LayoutParams(sizeWidth, (int) (sizeWidth / 1.5))); view.setScaleType(ImageView.ScaleType.CENTER_CROP); return view; }
public synchronized void cleanup(long size) { locked = true; while (SkylightApp.getPicturesDir().getUsableSpace() < size) { if (!files.isEmpty()) { PictureData victim = files.get(files.size() - 1); delete(victim); } else { break; } } locked = false; }
private Bitmap scaleAndRotate(Bitmap bmp, File file) { float w = bmp.getWidth(); float h = bmp.getHeight(); float s = Math.min(w, h); float l = Math.max(w, h); float sw = SkylightApp.getScreen().getSmallestWidth(); float lw = SkylightApp.getScreen().getLargestWidth(); float ratio = Math.min(1f, Math.max(sw / s, lw / l)); Bitmap scaled = Bitmap.createScaledBitmap(bmp, (int) (ratio * w), (int) (ratio * h), true); Bitmap rotated = scaled; try { ExifInterface ei = new ExifInterface(file.getAbsolutePath()); int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); Matrix mtx = new Matrix(); int scaledW = scaled.getWidth(); int scaledH = scaled.getHeight(); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: // rotate CCW mtx.preRotate(90); rotated = Bitmap.createBitmap(scaled, 0, 0, scaledW, scaledH, mtx, true); break; case ExifInterface.ORIENTATION_ROTATE_180: // rotate CCW mtx.preRotate(180); rotated = Bitmap.createBitmap(scaled, 0, 0, scaledW, scaledH, mtx, true); break; case ExifInterface.ORIENTATION_ROTATE_270: // rotate CW mtx.preRotate(-90); rotated = Bitmap.createBitmap(scaled, 0, 0, scaledW, scaledH, mtx, true); break; } } catch (IOException e) { } return rotated; }
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); } }
public File addToCache(MimeBodyPart bodyPart) throws IOException, MessagingException { File f = new File(SkylightApp.getTempDir(), bodyPart.getFileName()); cleanup(bodyPart.getSize() * MEMORY_COEFFICIENT); bodyPart.saveFile(f); return f; }
public ImagesGalleryAdapter(Context context) { this.context = context; this.fileHandler = SkylightApp.getFileHandler(); this.horizontalSpacing = dpToPx(16); this.service = Executors.newFixedThreadPool(20); }
private int dpToPx(int dp) { return Math.round( dp * ((float) SkylightApp.getScreen().getDensityDpi() / (float) DisplayMetrics.DENSITY_DEFAULT)); }