- class BitmapLoader extends AsyncTask<String, Void, Bitmap> {
- @SuppressLint({ "NewApi", "NewApi", "NewApi" }) // to avoid Lint errors since Android SDK r20
- @Override
- protected Bitmap doInBackground(String... params) {
- Bitmap result = null;
- if (params.length != 1) return result;
- String storagePath = params[0];
- try {
-
- BitmapFactory.Options options = new Options();
- options.inScaled = true;
- options.inPurgeable = true;
- options.inJustDecodeBounds = true;
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
- options.inPreferQualityOverSpeed = false;
- }
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
- options.inMutable = false;
- }
-
- result = BitmapFactory.decodeFile(storagePath, options);
- options.inJustDecodeBounds = false;
-
- int width = options.outWidth;
- int height = options.outHeight;
- int scale = 1;
- if (width >= 2048 || height >= 2048) {
- scale = (int) Math.ceil((Math.ceil(Math.max(height, width) / 2048.)));
- options.inSampleSize = scale;
- }
- Display display = getActivity().getWindowManager().getDefaultDisplay();
- Point size = new Point();
- int screenwidth;
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {
- display.getSize(size);
- screenwidth = size.x;
- } else {
- screenwidth = display.getWidth();
- }
-
- Log_OC.e("ASD", "W " + width + " SW " + screenwidth);
-
- if (width > screenwidth) {
- scale = (int) Math.ceil((float)width / screenwidth);
- options.inSampleSize = scale;
- }
-
- result = BitmapFactory.decodeFile(storagePath, options);
-
- Log_OC.e("ASD", "W " + options.outWidth + " SW " + options.outHeight);
-
- } catch (OutOfMemoryError e) {
- result = null;
- Log_OC.e(TAG, "Out of memory occured for file with size " + storagePath);
-
- } catch (NoSuchFieldError e) {
- result = null;
- Log_OC.e(TAG, "Error from access to unexisting field despite protection " + storagePath);
-
- } catch (Throwable t) {
- result = null;
- Log_OC.e(TAG, "Unexpected error while creating image preview " + storagePath, t);
- }
- return result;
- }
- @Override
- protected void onPostExecute(Bitmap result) {
- if (result != null && mPreview != null) {
- mPreview.setImageBitmap(result);
- }
- }
-
- }
-