2 * ownCloud Android client application
4 * Copyright (C) 2015 Tobias Kaminsky
5 * Copyright (C) 2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * adapted from: http://stephendnicholas.com/archives/974
23 package com
.owncloud
.android
.ui
.adapter
;
25 import android
.accounts
.Account
;
26 import android
.content
.ContentProvider
;
27 import android
.content
.ContentValues
;
28 import android
.content
.UriMatcher
;
29 import android
.database
.Cursor
;
30 import android
.graphics
.Bitmap
;
31 import android
.net
.Uri
;
32 import android
.os
.ParcelFileDescriptor
;
34 import com
.owncloud
.android
.MainApp
;
35 import com
.owncloud
.android
.authentication
.AccountUtils
;
36 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
37 import com
.owncloud
.android
.datamodel
.OCFile
;
38 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
39 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
41 import java
.io
.ByteArrayOutputStream
;
43 import java
.io
.FileNotFoundException
;
44 import java
.io
.FileOutputStream
;
45 import java
.io
.IOException
;
47 public class DiskLruImageCacheFileProvider
extends ContentProvider
{
48 private static String TAG
= FileDataStorageManager
.class.getSimpleName();
50 public static final String AUTHORITY
= "com.owncloud.imageCache.providerBeta";
53 public ParcelFileDescriptor
openFile(Uri uri
, String mode
) throws FileNotFoundException
{
54 Log_OC
.d(TAG
, "try to send: " + uri
);
55 Account account
= AccountUtils
.getCurrentOwnCloudAccount(MainApp
.getAppContext());
56 FileDataStorageManager fileDataStorageManager
= new FileDataStorageManager(account
,
57 MainApp
.getAppContext().getContentResolver());
59 OCFile ocFile
= fileDataStorageManager
.getFileByPath(uri
.getPath());
61 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(
62 String
.valueOf("r" + ocFile
.getRemoteId()));
64 // create a file to write bitmap data
65 File f
= new File(MainApp
.getAppContext().getCacheDir(), ocFile
.getFileName());
69 //Convert bitmap to byte array
70 ByteArrayOutputStream bos
= new ByteArrayOutputStream();
71 thumbnail
.compress(Bitmap
.CompressFormat
.JPEG
, 90, bos
);
72 byte[] bitmapdata
= bos
.toByteArray();
74 //write the bytes in file
75 FileOutputStream fos
= null
;
77 fos
= new FileOutputStream(f
);
78 } catch (FileNotFoundException e
) {
81 fos
.write(bitmapdata
);
85 } catch (IOException e
) {
89 return ParcelFileDescriptor
.open(f
, ParcelFileDescriptor
.MODE_READ_ONLY
);
94 public boolean onCreate() {
99 public Cursor
query(Uri uri
, String
[] projection
, String selection
, String
[] selectionArgs
, String sortOrder
) {
104 public String
getType(Uri uri
) {
109 public Uri
insert(Uri uri
, ContentValues values
) {
114 public int delete(Uri uri
, String selection
, String
[] selectionArgs
) {
119 public int update(Uri uri
, ContentValues values
, String selection
, String
[] selectionArgs
) {