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
.content
.ContentResolver
;
23 import android
.net
.Uri
;
24 import android
.os
.AsyncTask
;
26 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
29 import java
.io
.FileOutputStream
;
30 import java
.io
.InputStream
;
31 import java
.lang
.ref
.WeakReference
;
34 * AsyncTask to copy a file from a uri in a temporal file
36 public class CopyTmpFileAsyncTask
extends AsyncTask
<Object
, Void
, String
> {
38 private final String TAG
= CopyTmpFileAsyncTask
.class.getSimpleName();
39 private final WeakReference
<OnCopyTmpFileTaskListener
> mListener
;
42 public int getIndex(){
46 public CopyTmpFileAsyncTask(OnCopyTmpFileTaskListener listener
) {
47 mListener
= new WeakReference
<OnCopyTmpFileTaskListener
>(listener
);
53 * - String: path for saving the file into the app
54 * - int: index of upload
55 * - String: accountName
56 * - ContentResolver: content resolver
59 protected String
doInBackground(Object
[] params
) {
62 if (params
!= null
&& params
.length
== 5) {
63 Uri uri
= (Uri
) params
[0];
64 String filePath
= (String
) params
[1];
65 mIndex
= ((Integer
) params
[2]).intValue();
66 String accountName
= (String
) params
[3];
67 ContentResolver contentResolver
= (ContentResolver
) params
[4];
69 String fullTempPath
= FileStorageUtils
.getTemporalPath(accountName
) + filePath
;
70 InputStream inputStream
= null
;
71 FileOutputStream outputStream
= null
;
74 inputStream
= contentResolver
.openInputStream(uri
);
75 File cacheFile
= new File(fullTempPath
);
76 File tempDir
= cacheFile
.getParentFile();
77 if (!tempDir
.exists()) {
80 cacheFile
.createNewFile();
81 outputStream
= new FileOutputStream(fullTempPath
);
82 byte[] buffer
= new byte[4096];
86 while ((count
= inputStream
.read(buffer
)) > 0) {
87 outputStream
.write(buffer
, 0, count
);
93 result
= fullTempPath
;
94 } catch (Exception e
) {
95 Log_OC
.e(TAG
, "Exception ", e
);
96 if (inputStream
!= null
) {
99 } catch (Exception e1
) {
100 Log_OC
.e(TAG
, "Input Stream Exception ", e1
);
104 if (outputStream
!= null
) {
106 outputStream
.close();
107 } catch (Exception e1
) {
108 Log_OC
.e(TAG
, "Output Stream Exception ", e1
);
112 if (fullTempPath
!= null
) {
113 File f
= new File(fullTempPath
);
119 throw new IllegalArgumentException("Error in parameters number");
126 protected void onPostExecute(String result
) {
128 OnCopyTmpFileTaskListener listener
= mListener
.get();
131 listener
.onTmpFileCopied(result
, mIndex
);
136 * Interface to retrieve data from recognition task
138 public interface OnCopyTmpFileTaskListener
{
140 void onTmpFileCopied(String result
, int index
);