2 * ownCloud Android client application
4 * Copyright (C) 2015 ownCloud Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.utils
;
21 import android
.app
.Activity
;
22 import android
.content
.ContentResolver
;
23 import android
.content
.Context
;
24 import android
.net
.Uri
;
25 import android
.os
.AsyncTask
;
27 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
28 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
31 import java
.io
.FileOutputStream
;
32 import java
.io
.InputStream
;
33 import java
.lang
.ref
.WeakReference
;
36 * AsyncTask to copy a file from a uri in a temporal file
38 public class CopyTmpFileAsyncTask
extends AsyncTask
<Object
, Void
, String
> {
40 private final String TAG
= CopyTmpFileAsyncTask
.class.getSimpleName();
41 private final WeakReference
<OnCopyTmpFileTaskListener
> mListener
;
42 private String mAccountName
;
43 private ContentResolver mContentResolver
;
46 public int getIndex(){
50 public CopyTmpFileAsyncTask(Activity activity
) {
51 mContentResolver
= ((FileActivity
) activity
).getContentResolver();
52 mAccountName
= ((FileActivity
) activity
).getAccount().name
;
53 mListener
= new WeakReference
<OnCopyTmpFileTaskListener
>((OnCopyTmpFileTaskListener
)activity
);
57 protected String
doInBackground(Object
[] params
) {
60 if (params
.length
== 3) {
61 Uri uri
= (Uri
) params
[0];
62 String filePath
= (String
) params
[1];
63 mIndex
= ((Integer
) params
[2]).intValue();
65 String fullTempPath
= FileStorageUtils
.getTemporalPath(mAccountName
) + filePath
;
66 InputStream inputStream
= null
;
67 FileOutputStream outputStream
= null
;
70 inputStream
= mContentResolver
.openInputStream(uri
);
71 File cacheFile
= new File(fullTempPath
);
72 File tempDir
= cacheFile
.getParentFile();
73 if (!tempDir
.exists()) {
76 cacheFile
.createNewFile();
77 outputStream
= new FileOutputStream(fullTempPath
);
78 byte[] buffer
= new byte[4096];
82 while ((count
= inputStream
.read(buffer
)) > 0) {
83 outputStream
.write(buffer
, 0, count
);
89 result
= fullTempPath
;
90 } catch (Exception e
) {
91 Log_OC
.e(TAG
, "Exception ", e
);
92 if (inputStream
!= null
) {
95 } catch (Exception e1
) {
96 Log_OC
.e(TAG
, "Input Stream Exception ", e1
);
100 if (outputStream
!= null
) {
102 outputStream
.close();
103 } catch (Exception e1
) {
104 Log_OC
.e(TAG
, "Output Stream Exception ", e1
);
108 if (fullTempPath
!= null
) {
109 File f
= new File(fullTempPath
);
115 Log_OC
.e(TAG
, "Error in parameters number");
122 protected void onPostExecute(String result
) {
124 OnCopyTmpFileTaskListener listener
= mListener
.get();
127 listener
.OnCopyTmpFileTaskListener(result
, mIndex
);
132 * Interface to retrieve data from recognition task
134 public interface OnCopyTmpFileTaskListener
{
136 void OnCopyTmpFileTaskListener(String result
, int index
);