瀏覽代碼

Improved error messages in gallery for images that can not be loaded

David A. Velasco 12 年之前
父節點
當前提交
6cfa24f55a

二進制
res/drawable-hdpi/image_fail.png


二進制
res/drawable-ldpi/image_fail.png


二進制
res/drawable-mdpi/image_fail.png


+ 1 - 1
res/layout/file_preview.xml

@@ -42,7 +42,7 @@
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_margin="16dp"
-        android:contentDescription="@string/image_preview"
+        android:contentDescription="@string/preview_image_description"
         android:src="@drawable/owncloud_logo" />
     
    	<VideoView  

+ 13 - 3
res/layout/preview_image_fragment.xml

@@ -34,7 +34,6 @@
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:background="@color/owncloud_white"
-    android:gravity="center"
     tools:context=".ui.fragment.PreviewImageFragment" >
 
     <ImageView
@@ -42,7 +41,18 @@
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_margin="0dp"
-        android:contentDescription="@string/image_preview"
-        android:src="@drawable/owncloud_logo" />
+        android:layout_centerInParent="true"
+        android:contentDescription="@string/preview_image_description"
+        android:src="@drawable/image_fail" />
+    
+    <TextView
+        android:id="@+id/message"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_centerHorizontal="true"
+        android:layout_below="@id/image"
+        android:layout_margin="40dp"
+        android:text="@string/placeholder_sentence"
+        />
     
 </RelativeLayout>

+ 4 - 2
res/values/strings.xml

@@ -89,6 +89,7 @@
     <string name="common_exit">Leave %1$s</string>
     <string name="common_error">Error</string>
     <string name="common_loading">Loading &#8230;</string>
+    <string name="common_error_unknown">Unkown error</string>
     <string name="about_title">About</string>
     
     <string name="delete_account">Delete account</string>
@@ -276,8 +277,9 @@
     <string name="conflict_overwrite">Overwrite</string>
     <string name="conflict_dont_upload">Don\'t upload</string>
     
-    <string name="image_preview">Image preview</string>
-    <string name="preview_image_loading"></string>
+    <string name="preview_image_description">Image preview</string>
+    <string name="preview_image_error_unknown_format">This image can not be shown</string>
+    <string name="preview_image_error_out_of_memory">"Not enough memory to show this image</string>
     
     <!-- we need to improve the communication of errors to the user -->
     <string name="error__upload__local_file_not_copied">%1$s could not be copied to %2$s local directory</string>

+ 10 - 1
src/com/owncloud/android/ui/preview/PreviewImageActivity.java

@@ -17,6 +17,8 @@
  */
 package com.owncloud.android.ui.preview;
 
+import org.apache.commons.httpclient.methods.PostMethod;
+
 import android.accounts.Account;
 import android.app.Dialog;
 import android.app.ProgressDialog;
@@ -28,6 +30,7 @@ import android.content.IntentFilter;
 import android.content.ServiceConnection;
 import android.os.Bundle;
 import android.os.IBinder;
+import android.support.v4.app.Fragment;
 import android.support.v4.view.ViewPager;
 import android.util.Log;
 import android.view.MotionEvent;
@@ -234,14 +237,15 @@ public class PreviewImageActivity extends SherlockFragmentActivity implements Fi
     @Override
     protected void onResume() {
         super.onResume();
+        Log.e(TAG, "ACTIVITY, ONRESUME");
         mDownloadFinishReceiver = new DownloadFinishReceiver();
         IntentFilter filter = new IntentFilter(FileDownloader.DOWNLOAD_FINISH_MESSAGE);
         registerReceiver(mDownloadFinishReceiver, filter);
     }
 
-    
     @Override
     protected void onPostResume() {
+        Log.e(TAG, "ACTIVITY, ONPOSTRESUME");
         super.onPostResume();
     }
     
@@ -346,6 +350,11 @@ public class PreviewImageActivity extends SherlockFragmentActivity implements Fi
             getSupportActionBar().setTitle(currentFile.getFileName());
             if (!currentFile.isDown()) {
                 requestForDownload(currentFile);
+            /*} else {
+                FileFragment fragment = mPreviewImagePagerAdapter.getFragmentAt(mViewPager.getCurrentItem());
+                if (fragment instanceof PreviewImageFragment) {
+                    ((PreviewImageFragment)fragment).showError();
+                }*/
             }
         }
     }

+ 81 - 24
src/com/owncloud/android/ui/preview/PreviewImageFragment.java

@@ -45,6 +45,7 @@ import android.view.View.OnTouchListener;
 import android.view.ViewGroup;
 import android.webkit.MimeTypeMap;
 import android.widget.ImageView;
+import android.widget.TextView;
 import android.widget.Toast;
 
 import com.actionbarsherlock.app.SherlockFragment;
@@ -77,7 +78,7 @@ import eu.alefzero.webdav.WebdavUtils;
  */
 public class PreviewImageFragment extends SherlockFragment implements   FileFragment, 
                                                                         OnRemoteOperationListener, 
-                                                                        ConfirmationDialogFragment.ConfirmationDialogFragmentListener{
+                                                                        ConfirmationDialogFragment.ConfirmationDialogFragmentListener {
     public static final String EXTRA_FILE = "FILE";
     public static final String EXTRA_ACCOUNT = "ACCOUNT";
 
@@ -86,6 +87,8 @@ public class PreviewImageFragment extends SherlockFragment implements   FileFrag
     private Account mAccount;
     private FileDataStorageManager mStorageManager;
     private ImageView mImageView;
+    private TextView mMessageView;
+
     public Bitmap mBitmap = null;
     
     private Handler mHandler;
@@ -94,6 +97,7 @@ public class PreviewImageFragment extends SherlockFragment implements   FileFrag
     private static final String TAG = PreviewImageFragment.class.getSimpleName();
 
     private boolean mIgnoreFirstSavedState;
+
     
     /**
      * Creates a fragment to preview an image.
@@ -147,7 +151,9 @@ public class PreviewImageFragment extends SherlockFragment implements   FileFrag
         super.onCreateView(inflater, container, savedInstanceState);
         mView = inflater.inflate(R.layout.preview_image_fragment, container, false);
         mImageView = (ImageView)mView.findViewById(R.id.image);
-        mView.setOnTouchListener((OnTouchListener)getActivity());   // WATCH OUT
+        mView.setOnTouchListener((OnTouchListener)getActivity());   // WATCH OUT THAT CAST
+        mMessageView = (TextView)mView.findViewById(R.id.message);
+        mMessageView.setVisibility(View.GONE);
         return mView;
     }
     
@@ -205,7 +211,7 @@ public class PreviewImageFragment extends SherlockFragment implements   FileFrag
     public void onStart() {
         super.onStart();
         if (mFile != null) {
-           BitmapLoader bl = new BitmapLoader(mImageView);
+           BitmapLoader bl = new BitmapLoader(mImageView, mMessageView);
            bl.execute(new String[]{mFile.getStoragePath()});
         }
     }
@@ -271,6 +277,7 @@ public class PreviewImageFragment extends SherlockFragment implements   FileFrag
     @Override
     public void onResume() {
         super.onResume();
+        Log.e(TAG, "FRAGMENT, ONRESUME");
         /*
         mDownloadFinishReceiver = new DownloadFinishReceiver();
         IntentFilter filter = new IntentFilter(
@@ -460,6 +467,16 @@ public class PreviewImageFragment extends SherlockFragment implements   FileFrag
          * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes.
          */
         private final WeakReference<ImageView> mImageViewRef;
+
+        /**
+         * Weak reference to the target {@link TextView} where error messages will be written.
+         * 
+         * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes.
+         */
+        private final WeakReference<TextView> mMessageViewRef;
+
+        
+        private Throwable mThrowable;
         
         
         /**
@@ -467,8 +484,10 @@ public class PreviewImageFragment extends SherlockFragment implements   FileFrag
          * 
          * @param imageView     Target {@link ImageView} where the bitmap will be loaded into.
          */
-        public BitmapLoader(ImageView imageView) {
+        public BitmapLoader(ImageView imageView, TextView messageView) {
             mImageViewRef = new WeakReference<ImageView>(imageView);
+            mMessageViewRef = new WeakReference<TextView>(messageView);
+            mThrowable = null;
         }
         
         
@@ -497,44 +516,46 @@ public class PreviewImageFragment extends SherlockFragment implements   FileFrag
                 int width = options.outWidth;
                 int height = options.outHeight;
                 int scale = 1;
+                
+                /*
                 if (width >= 2048 || height >= 2048) {  
                     // try to scale down the image to save memory  
                     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;
+                int screenWidth;
+                int screenHeight;
                 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {
                     display.getSize(size);
-                    screenwidth = size.x;
+                    screenWidth = size.x;
+                    screenHeight = size.y;
                 } else {
-                    screenwidth = display.getWidth();
+                    screenWidth = display.getWidth();
+                    screenHeight = display.getHeight();
                 }
 
-                Log.d(TAG, "image width: " + width + ", screen width: " + screenwidth);
-
-                if (width > screenwidth) {
+                if (width > screenWidth) {
                     // second try to scale down the image , this time depending upon the screen size; WTF... 
-                    scale = (int) Math.ceil((float)width / screenwidth);
+                    scale = (int) Math.ceil((float)width / screenWidth);
                     options.inSampleSize = scale;
                 }
+                if (height > screenHeight) {
+                    scale = Math.max(scale, (int) Math.ceil((float)height / screenHeight));
+                }
+                
 
                 // really load the bitmap
                 options.inJustDecodeBounds = false; // the next decodeFile call will be real
                 result = BitmapFactory.decodeFile(storagePath, options);
-                Log.e(TAG, "loaded width: " + options.outWidth + ", loaded height: " + options.outHeight);
+                //Log.d(TAG, "Image loaded - width: " + options.outWidth + ", loaded height: " + options.outHeight);
 
-            } catch (OutOfMemoryError e) {
-                result = null;
-                Log.e(TAG, "Out of memory occured for file with size " + storagePath);
-                
-            } catch (NoSuchFieldError e) {
-                result = null;
-                Log.e(TAG, "Error from access to unexisting field despite protection " + storagePath);
-                
             } catch (Throwable t) {
                 result = null;
+                mThrowable = t; // error processing is delayed to #onPostExecute(Bitmap)
                 Log.e(TAG, "Unexpected error while creating image preview " + storagePath, t);
             }
             return result;
@@ -542,11 +563,46 @@ public class PreviewImageFragment extends SherlockFragment implements   FileFrag
         
         @Override
         protected void onPostExecute(Bitmap result) {
-            if (result != null && mImageViewRef != null) {
+            if (mImageViewRef != null && result != null) {
                 final ImageView imageView = mImageViewRef.get();
-                imageView.setImageBitmap(result);
-                mBitmap  = result;
+                if (imageView != null) {
+                    imageView.setImageBitmap(result);
+                    mBitmap  = result;
+                    if (mMessageViewRef != null) {
+                        final TextView messageView = mMessageViewRef.get();
+                        if (messageView != null) {
+                            messageView.setVisibility(View.GONE);
+                        }
+                    }
+                } // else , silently finish, the fragment was destroyed
+                
+            } else if (mMessageViewRef != null && result == null) {
+                // error
+                int messageId;
+                if (mThrowable == null) {
+                    messageId = R.string.preview_image_error_unknown_format;
+                    Log.e(TAG, "File could not be loaded as a bitmap: " + mFile.getStoragePath());
+                
+                } else if (mThrowable instanceof OutOfMemoryError) {
+                    messageId = R.string.preview_image_error_unknown_format;
+                    Log.e(TAG, "Out of memory occured for file " + mFile.getStoragePath(), mThrowable);
+                    
+                } else if (mThrowable instanceof NoSuchFieldError) {
+                    messageId = R.string.common_error_unknown;
+                    Log.e(TAG, "Error from access to unexisting field despite protection; file " + mFile.getStoragePath(), mThrowable);
+                    
+                } else {
+                    messageId = R.string.common_error_unknown;
+                    Log.e(TAG, "Unexpected error loading " + mFile.getStoragePath(), mThrowable);
+                }
+                final TextView messageView = mMessageViewRef.get();
+                if (messageView != null) {
+                    messageView.setText(messageId);
+                    messageView.setVisibility(View.VISIBLE);
+                } // else , silently finish, the fragment was destroyed
+                    
             }
+            
         }
         
     }
@@ -561,6 +617,7 @@ public class PreviewImageFragment extends SherlockFragment implements   FileFrag
         return (file != null && file.isImage());
     }
 
+    
     /**
      * {@inheritDoc}
      */
@@ -596,5 +653,5 @@ public class PreviewImageFragment extends SherlockFragment implements   FileFrag
         container.finish();
     }
     
-
+    
 }