2 * ownCloud Android client application
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/>.
20 package com
.owncloud
.android
.utils
;
22 import android
.app
.Activity
;
23 import android
.content
.ContentResolver
;
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
;
44 public int getIndex(){
48 public CopyTmpFileAsyncTask(Activity activity
) {
49 mListener
= new WeakReference
<OnCopyTmpFileTaskListener
>((OnCopyTmpFileTaskListener
)activity
);
55 * - String: path for saving the file into the app
56 * - int: index of upload
57 * - String: accountName
58 * - ContentResolver: content resolver
61 protected String
doInBackground(Object
[] params
) {
64 if (params
!= null
&& params
.length
== 5) {
65 Uri uri
= (Uri
) params
[0];
66 String filePath
= (String
) params
[1];
67 mIndex
= ((Integer
) params
[2]).intValue();
68 String accountName
= (String
) params
[3];
69 ContentResolver contentResolver
= (ContentResolver
) params
[4];
71 String fullTempPath
= FileStorageUtils
.getTemporalPath(accountName
) + filePath
;
72 InputStream inputStream
= null
;
73 FileOutputStream outputStream
= null
;
76 inputStream
= contentResolver
.openInputStream(uri
);
77 File cacheFile
= new File(fullTempPath
);
78 File tempDir
= cacheFile
.getParentFile();
79 if (!tempDir
.exists()) {
82 cacheFile
.createNewFile();
83 outputStream
= new FileOutputStream(fullTempPath
);
84 byte[] buffer
= new byte[4096];
88 while ((count
= inputStream
.read(buffer
)) > 0) {
89 outputStream
.write(buffer
, 0, count
);
95 result
= fullTempPath
;
96 } catch (Exception e
) {
97 Log_OC
.e(TAG
, "Exception ", e
);
98 if (inputStream
!= null
) {
101 } catch (Exception e1
) {
102 Log_OC
.e(TAG
, "Input Stream Exception ", e1
);
106 if (outputStream
!= null
) {
108 outputStream
.close();
109 } catch (Exception e1
) {
110 Log_OC
.e(TAG
, "Output Stream Exception ", e1
);
114 if (fullTempPath
!= null
) {
115 File f
= new File(fullTempPath
);
121 throw new IllegalArgumentException("Error in parameters number");
128 protected void onPostExecute(String result
) {
130 OnCopyTmpFileTaskListener listener
= mListener
.get();
133 listener
.onTmpFileCopied(result
, mIndex
);
138 * Interface to retrieve data from recognition task
140 public interface OnCopyTmpFileTaskListener
{
142 void onTmpFileCopied(String result
, int index
);