DiskLruImageCacheFileProvider.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2017 Tobias Kaminsky
  6. * Copyright (C) 2017 Nextcloud GmbH.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.providers;
  22. import android.accounts.Account;
  23. import android.content.ContentProvider;
  24. import android.content.ContentValues;
  25. import android.database.Cursor;
  26. import android.database.MatrixCursor;
  27. import android.graphics.Bitmap;
  28. import android.net.Uri;
  29. import android.os.ParcelFileDescriptor;
  30. import android.provider.OpenableColumns;
  31. import android.support.annotation.NonNull;
  32. import com.owncloud.android.MainApp;
  33. import com.owncloud.android.authentication.AccountUtils;
  34. import com.owncloud.android.datamodel.FileDataStorageManager;
  35. import com.owncloud.android.datamodel.OCFile;
  36. import com.owncloud.android.datamodel.ThumbnailsCacheManager;
  37. import com.owncloud.android.lib.common.utils.Log_OC;
  38. import java.io.ByteArrayOutputStream;
  39. import java.io.File;
  40. import java.io.FileNotFoundException;
  41. import java.io.FileOutputStream;
  42. public class DiskLruImageCacheFileProvider extends ContentProvider {
  43. public static final String TAG = DiskLruImageCacheFileProvider.class.getSimpleName();
  44. @Override
  45. public boolean onCreate() {
  46. return true;
  47. }
  48. private OCFile getFile(Uri uri) {
  49. Account account = AccountUtils.getCurrentOwnCloudAccount(MainApp.getAppContext());
  50. FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(account,
  51. MainApp.getAppContext().getContentResolver());
  52. return fileDataStorageManager.getFileByPath(uri.getPath());
  53. }
  54. @Override
  55. public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException {
  56. OCFile ocFile = getFile(uri);
  57. Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
  58. String.valueOf(ThumbnailsCacheManager.PREFIX_RESIZED_IMAGE + ocFile.getRemoteId()));
  59. // fallback to thumbnail
  60. if (thumbnail == null) {
  61. thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
  62. String.valueOf(ThumbnailsCacheManager.PREFIX_THUMBNAIL + ocFile.getRemoteId()));
  63. }
  64. // fallback to default image
  65. if (thumbnail == null) {
  66. thumbnail = ThumbnailsCacheManager.mDefaultImg;
  67. }
  68. // create a file to write bitmap data
  69. File f = new File(MainApp.getAppContext().getCacheDir(), ocFile.getFileName());
  70. try {
  71. f.createNewFile();
  72. //Convert bitmap to byte array
  73. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  74. thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bos);
  75. byte[] bitmapData = bos.toByteArray();
  76. //write the bytes in file
  77. try (FileOutputStream fos = new FileOutputStream(f)){
  78. fos.write(bitmapData);
  79. } catch (FileNotFoundException e) {
  80. Log_OC.e(TAG, "File not found: " + e.getMessage());
  81. }
  82. } catch (Exception e) {
  83. Log_OC.e(TAG, "Error opening file: " + e.getMessage());
  84. }
  85. return ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
  86. }
  87. @Override
  88. public String getType(@NonNull Uri uri) {
  89. OCFile ocFile = getFile(uri);
  90. return ocFile.getMimetype();
  91. }
  92. @Override
  93. public Cursor query(@NonNull Uri uri, String[] arg1, String arg2, String[] arg3, String arg4) {
  94. MatrixCursor cursor = null;
  95. OCFile ocFile = getFile(uri);
  96. File file = new File(MainApp.getAppContext().getCacheDir(), ocFile.getFileName());
  97. if (file.exists()) {
  98. cursor = new MatrixCursor(new String[] {
  99. OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE });
  100. cursor.addRow(new Object[] { uri.getLastPathSegment(),
  101. file.length() });
  102. }
  103. return cursor;
  104. }
  105. @Override
  106. public Uri insert(@NonNull Uri uri, ContentValues values) {
  107. return null;
  108. }
  109. @Override
  110. public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) {
  111. return 0;
  112. }
  113. @Override
  114. public int update(@NonNull Uri uri, ContentValues values, String selection, String[] selectionArgs) {
  115. return 0;
  116. }
  117. }