load bitmap asynchronously on preview
authorBartek Przybylski <bart.p.pl@gmail.com>
Thu, 12 Jul 2012 17:52:56 +0000 (19:52 +0200)
committerBartek Przybylski <bart.p.pl@gmail.com>
Thu, 12 Jul 2012 17:52:56 +0000 (19:52 +0200)
src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java

index efee656..db91641 100644 (file)
@@ -61,6 +61,7 @@ import android.graphics.Point;
 import android.graphics.drawable.BitmapDrawable;\r
 import android.graphics.drawable.Drawable;\r
 import android.net.Uri;\r
+import android.os.AsyncTask;\r
 import android.os.Bundle;\r
 import android.os.Environment;\r
 import android.os.Handler;\r
@@ -111,6 +112,7 @@ public class FileDetailFragment extends SherlockFragment implements
     private View mView;\r
     private OCFile mFile;\r
     private Account mAccount;\r
+    private ImageView mPreview;\r
     \r
     private DownloadFinishReceiver mDownloadFinishReceiver;\r
 \r
@@ -169,6 +171,7 @@ public class FileDetailFragment extends SherlockFragment implements
             //mView.findViewById(R.id.fdShareBtn).setOnClickListener(this);\r
             mView.findViewById(R.id.fdRenameBtn).setOnClickListener(this);\r
             mView.findViewById(R.id.fdRemoveBtn).setOnClickListener(this);\r
+            mPreview = (ImageView)mView.findViewById(R.id.fdPreview);\r
         }\r
         \r
         updateFileDetails();\r
@@ -200,6 +203,10 @@ public class FileDetailFragment extends SherlockFragment implements
         super.onPause();\r
         getActivity().unregisterReceiver(mDownloadFinishReceiver);\r
         mDownloadFinishReceiver = null;\r
+        if (mPreview != null) {\r
+            mPreview = null;\r
+            System.gc();\r
+        }\r
     }\r
 \r
     @Override\r
@@ -322,69 +329,9 @@ public class FileDetailFragment extends SherlockFragment implements
             \r
             if (mFile.getStoragePath() != null) {\r
                 // Update preview\r
-                ImageView preview = (ImageView) getView().findViewById(R.id.fdPreview);\r
-                try {\r
-                    if (mFile.getMimetype().startsWith("image/")) {\r
-                        BitmapFactory.Options options = new Options();\r
-                        options.inScaled = true;\r
-                        options.inPurgeable = true;\r
-                        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {\r
-                            options.inPreferQualityOverSpeed = false;\r
-                        }\r
-                        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {\r
-                            options.inMutable = false;\r
-                        }\r
-\r
-                        Bitmap bmp = BitmapFactory.decodeFile(mFile.getStoragePath(), options);\r
-\r
-                        if (bmp != null) {\r
-                            int width = options.outWidth;\r
-                            int height = options.outHeight;\r
-                            int scale = 1;\r
-                            boolean recycle = false;\r
-                            if (width >= 2048 || height >= 2048) {\r
-                                scale = (int) (Math.ceil(Math.max(height, width)/2048.));\r
-                                options.inSampleSize = scale;\r
-                                recycle = true;\r
-                            }\r
-                                Display display = getActivity().getWindowManager().getDefaultDisplay();\r
-                                Point size = new Point();\r
-                                int screenwidth;\r
-                                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {\r
-                                    display.getSize(size);\r
-                                    screenwidth = size.x;\r
-                                } else {\r
-                                    screenwidth = display.getWidth();\r
-                                }\r
-\r
-                                Log.e("ASD", "W " + width + " SW " + screenwidth);\r
-\r
-                                if (width > screenwidth) {\r
-                                    scale = (int) (Math.ceil(Math.max(height, width)/screenwidth));\r
-                                    options.inSampleSize = scale;\r
-                                    recycle = true;\r
-                                }\r
-                            \r
-\r
-                            if (recycle) bmp.recycle();\r
-                            bmp = BitmapFactory.decodeFile(mFile.getStoragePath(), options);\r
-                            \r
-                        }\r
-                        if (bmp != null) {\r
-                            preview.setImageBitmap(bmp);\r
-                        }\r
-                    }\r
-                } catch (OutOfMemoryError e) {\r
-                    preview.setVisibility(View.INVISIBLE);\r
-                    Log.e(TAG, "Out of memory occured for file with size " + mFile.getFileLength());\r
-                    \r
-                } catch (NoSuchFieldError e) {\r
-                    preview.setVisibility(View.INVISIBLE);\r
-                    Log.e(TAG, "Error from access to unexisting field despite protection " + mFile.getFileLength());\r
-                    \r
-                } catch (Throwable t) {\r
-                    preview.setVisibility(View.INVISIBLE);\r
-                    Log.e(TAG, "Unexpected error while creating image preview " + mFile.getFileLength(), t);\r
+                if (mFile.getMimetype().startsWith("image/")) {\r
+                    BitmapLoader bl = new BitmapLoader();\r
+                    bl.execute(new String[]{mFile.getStoragePath()});\r
                 }\r
                 \r
                 // Change download button to open button\r
@@ -890,5 +837,81 @@ public class FileDetailFragment extends SherlockFragment implements
         \r
     }\r
     \r
+    class BitmapLoader extends AsyncTask<String, Void, Bitmap> {\r
+        @Override\r
+        protected Bitmap doInBackground(String... params) {\r
+            Bitmap result = null;\r
+            if (params.length != 1) return result;\r
+            String storagePath = params[0];\r
+            try {\r
+\r
+                BitmapFactory.Options options = new Options();\r
+                options.inScaled = true;\r
+                options.inPurgeable = true;\r
+                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {\r
+                    options.inPreferQualityOverSpeed = false;\r
+                }\r
+                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {\r
+                    options.inMutable = false;\r
+                }\r
+\r
+                result = BitmapFactory.decodeFile(storagePath, options);\r
+\r
+                if (result != null) {\r
+                    int width = options.outWidth;\r
+                    int height = options.outHeight;\r
+                    int scale = 1;\r
+                    boolean recycle = false;\r
+                    if (width >= 2048 || height >= 2048) {\r
+                        scale = (int) (Math.ceil(Math.max(height, width) / 2048.));\r
+                        options.inSampleSize = scale;\r
+                        recycle = true;\r
+                    }\r
+                    Display display = getActivity().getWindowManager().getDefaultDisplay();\r
+                    Point size = new Point();\r
+                    int screenwidth;\r
+                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {\r
+                        display.getSize(size);\r
+                        screenwidth = size.x;\r
+                    } else {\r
+                        screenwidth = display.getWidth();\r
+                    }\r
+\r
+                    Log.e("ASD", "W " + width + " SW " + screenwidth);\r
+\r
+                    if (width > screenwidth) {\r
+                        scale = (int) (Math.ceil(Math.max(height, width) / screenwidth));\r
+                        options.inSampleSize = scale;\r
+                        recycle = true;\r
+                    }\r
+\r
+                    if (recycle)\r
+                        result.recycle();\r
+                    result = BitmapFactory.decodeFile(storagePath, options);\r
+                }\r
+\r
+            } catch (OutOfMemoryError e) {\r
+                result = null;\r
+                Log.e(TAG, "Out of memory occured for file with size " + storagePath);\r
+                \r
+            } catch (NoSuchFieldError e) {\r
+                result = null;\r
+                Log.e(TAG, "Error from access to unexisting field despite protection " + storagePath);\r
+                \r
+            } catch (Throwable t) {\r
+                result = null;\r
+                Log.e(TAG, "Unexpected error while creating image preview " + storagePath, t);\r
+            }\r
+            return result;\r
+        }\r
+        @Override\r
+        protected void onPostExecute(Bitmap result) {\r
+            if (result != null) {\r
+                mPreview.setImageBitmap(result);\r
+            }\r
+        }\r
+        \r
+    }\r
+    \r
 \r
 }\r