- mFile.setNeedsUpdateThumbnail(false);
- mStorageManager.saveFile(mFile);
- }
-
- } else {
- // Download thumbnail from server
- if (mClient != null && mServerVersion != null) {
- OwnCloudVersion serverOCVersion = new OwnCloudVersion(mServerVersion);
- if (serverOCVersion.compareTo(new OwnCloudVersion(MINOR_SERVER_VERSION_FOR_THUMBS)) >= 0) {
- try {
- int status = -1;
-
- String uri = mClient.getBaseUri() + "/index.php/apps/files/api/v1/thumbnail/" +
- px + "/" + px + Uri.encode(mFile.getRemotePath(), "/");
- Log_OC.d("Thumbnail", "URI: " + uri);
- GetMethod get = new GetMethod(uri);
- status = mClient.executeMethod(get);
- if (status == HttpStatus.SC_OK) {
- byte[] bytes = get.getResponseBody();
- Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
- thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px);
-
- // Add thumbnail to cache
- if (thumbnail != null) {
- addBitmapToCache(imageKey, thumbnail);
- }
+ }catch(Throwable t){
+ // the app should never break due to a problem with thumbnails
+ Log_OC.e(TAG, "Generation of thumbnail for " + mFile + " failed", t);
+ if (t instanceof OutOfMemoryError) {
+ System.gc();
+ }
+ }
+
+ return thumbnail;
+ }
+
+ protected void onPostExecute(Bitmap bitmap){
+ if (isCancelled()) {
+ bitmap = null;
+ }
+
+ if (bitmap != null) {
+ final ImageView imageView = mImageViewReference.get();
+ final ThumbnailGenerationTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
+ if (this == bitmapWorkerTask) {
+ String tagId = "";
+ if (mFile instanceof OCFile){
+ tagId = String.valueOf(((OCFile)mFile).getFileId());
+ } else if (mFile instanceof File){
+ tagId = String.valueOf(mFile.hashCode());
+ }
+ if (String.valueOf(imageView.getTag()).equals(tagId)) {
+ imageView.setImageBitmap(bitmap);
+ }
+ }
+ }
+ }
+
+ /**
+ * Add thumbnail to cache
+ * @param imageKey: thumb key
+ * @param bitmap: image for extracting thumbnail
+ * @param path: image path
+ * @param px: thumbnail dp
+ * @return Bitmap
+ */
+ private Bitmap addThumbnailToCache(String imageKey, Bitmap bitmap, String path, int px){
+
+ Bitmap thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px);
+
+ // Rotate image, obeying exif tag
+ thumbnail = BitmapUtils.rotateImage(thumbnail,path);
+
+ // Add thumbnail to cache
+ addBitmapToCache(imageKey, thumbnail);
+
+ return thumbnail;
+ }
+
+ /**
+ * Converts size of file icon from dp to pixel
+ * @return int
+ */
+ private int getThumbnailDimension(){
+ // Converts dp to pixel
+ Resources r = MainApp.getAppContext().getResources();
+ return Math.round(r.getDimension(R.dimen.file_icon_size_grid));
+ }
+
+ private Bitmap doOCFileInBackground() {
+ OCFile file = (OCFile)mFile;
+
+ final String imageKey = String.valueOf(file.getRemoteId());
+
+ // Check disk cache in background thread
+ Bitmap thumbnail = getBitmapFromDiskCache(imageKey);
+
+ // Not found in disk cache
+ if (thumbnail == null || file.needsUpdateThumbnail()) {
+
+ int px = getThumbnailDimension();
+
+ if (file.isDown()) {
+ Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile(
+ file.getStoragePath(), px, px);
+
+ if (bitmap != null) {
+ thumbnail = addThumbnailToCache(imageKey, bitmap, file.getStoragePath(), px);
+
+ file.setNeedsUpdateThumbnail(false);
+ mStorageManager.saveFile(file);
+ }
+
+ } else {
+ // Download thumbnail from server
+ OwnCloudVersion serverOCVersion = AccountUtils.getServerVersion(mAccount);
+ if (mClient != null && serverOCVersion != null) {
+ if (serverOCVersion.supportsRemoteThumbnails()) {
+ try {
+ String uri = mClient.getBaseUri() + "" +
+ "/index.php/apps/files/api/v1/thumbnail/" +
+ px + "/" + px + Uri.encode(file.getRemotePath(), "/");
+ Log_OC.d("Thumbnail", "URI: " + uri);
+ GetMethod get = new GetMethod(uri);
+ int status = mClient.executeMethod(get);
+ if (status == HttpStatus.SC_OK) {
+// byte[] bytes = get.getResponseBody();
+// Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0,
+// bytes.length);
+ InputStream inputStream = get.getResponseBodyAsStream();
+ Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
+ thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px);
+
+ // Add thumbnail to cache
+ if (thumbnail != null) {
+ addBitmapToCache(imageKey, thumbnail);