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
;
45 public CopyTmpFileAsyncTask(Activity activity
) {
46 mContentResolver
= ((FileActivity
) activity
).getContentResolver();
47 mAccountName
= ((FileActivity
) activity
).getAccount().name
;
48 mListener
= new WeakReference
<OnCopyTmpFileTaskListener
>((OnCopyTmpFileTaskListener
)activity
);
52 protected String
doInBackground(Object
[] params
) {
55 if (params
.length
== 2) {
56 Uri uri
= (Uri
) params
[0];
57 String filePath
= (String
) params
[1];
59 String fullTempPath
= FileStorageUtils
.getTemporalPath(mAccountName
) + filePath
;
60 InputStream inputStream
= null
;
61 FileOutputStream outputStream
= null
;
64 inputStream
= mContentResolver
.openInputStream(uri
);
65 File cacheFile
= new File(fullTempPath
);
66 File tempDir
= cacheFile
.getParentFile();
67 if (!tempDir
.exists()) {
70 cacheFile
.createNewFile();
71 outputStream
= new FileOutputStream(fullTempPath
);
72 byte[] buffer
= new byte[4096];
76 while ((count
= inputStream
.read(buffer
)) > 0) {
77 outputStream
.write(buffer
, 0, count
);
83 result
= fullTempPath
;
84 } catch (Exception e
) {
85 Log_OC
.e(TAG
, "Exception ", e
);
86 if (inputStream
!= null
) {
89 } catch (Exception e1
) {
90 Log_OC
.e(TAG
, "Input Stream Exception ", e1
);
94 if (outputStream
!= null
) {
97 } catch (Exception e1
) {
98 Log_OC
.e(TAG
, "Output Stream Exception ", e1
);
102 if (fullTempPath
!= null
) {
103 File f
= new File(fullTempPath
);
109 Log_OC
.e(TAG
, "Error in parameters number");
116 protected void onPostExecute(String result
) {
118 OnCopyTmpFileTaskListener listener
= mListener
.get();
121 listener
.OnCopyTmpFileTaskListener(result
);
126 * Interface to retrieve data from recognition task
128 public interface OnCopyTmpFileTaskListener
{
130 void OnCopyTmpFileTaskListener(String result
);