From: Bartek Przybylski Date: Sun, 25 Dec 2011 12:04:18 +0000 (+0100) Subject: downloading file X-Git-Tag: oc-android-1.4.3~498 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/a038e0c55b6e5c625f364b63d5a5e6c376bb1569?ds=inline downloading file --- diff --git a/src/eu/alefzero/owncloud/FileDownloader.java b/src/eu/alefzero/owncloud/FileDownloader.java index 0e8e03b0..0c550445 100644 --- a/src/eu/alefzero/owncloud/FileDownloader.java +++ b/src/eu/alefzero/owncloud/FileDownloader.java @@ -81,11 +81,11 @@ public class FileDownloader extends Service { PendingIntent pi = PendingIntent.getActivity(this, 1, new Intent(this, OwnCloudMainScreen.class), 0); n.setLatestEventInfo(this, "A", "B", pi); nm.notify(1, n); - + File sdCard = Environment.getExternalStorageDirectory(); File dir = new File (sdCard.getAbsolutePath() + "/owncloud"); dir.mkdirs(); - File file = new File(dir, "filename"); + File file = new File(dir, file_path.replace('/', '.')); wdc.downloadFile(file_path, file); diff --git a/src/eu/alefzero/owncloud/OwnCloudMainScreen.java b/src/eu/alefzero/owncloud/OwnCloudMainScreen.java index 65592b19..c89672ce 100644 --- a/src/eu/alefzero/owncloud/OwnCloudMainScreen.java +++ b/src/eu/alefzero/owncloud/OwnCloudMainScreen.java @@ -278,18 +278,9 @@ public class OwnCloudMainScreen extends ListActivity { } getListView().invalidate(); } else { - try { Intent i = (Intent) getListAdapter().getItem(position); if (i.hasExtra("toDownload")) { - Uri data = Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath() + "/owncloud/filename"); - Log.d("DUPA", data.toString()); - File f = new File(data.toString()); - FileInputStream fis = new FileInputStream(f); - byte buffer[] = new byte[512]; - fis.read(buffer); - Log.d("DUPA", new String(buffer)); - Intent intent = new Intent(this, FileDownloader.class); intent.putExtra(FileDownloader.EXTRA_FILE_PATH, "/"+((TextView)findViewById(R.id.textView1)).getText().toString()); intent.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount); @@ -301,13 +292,7 @@ public class OwnCloudMainScreen extends ListActivity { startActivity(i); }*/ } - } catch (ClassCastException e) {} catch (FileNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + } } diff --git a/src/eu/alefzero/owncloud/Uploader.java b/src/eu/alefzero/owncloud/Uploader.java index 66dec569..5a6a4a22 100644 --- a/src/eu/alefzero/owncloud/Uploader.java +++ b/src/eu/alefzero/owncloud/Uploader.java @@ -1,5 +1,9 @@ package eu.alefzero.owncloud; +import java.io.File; +import java.net.FileNameMap; +import java.net.URI; +import java.net.URLConnection; import java.util.ArrayList; import java.util.Stack; @@ -173,7 +177,7 @@ public class Uploader extends ListActivity implements OnItemClickListener, andro throw new IllegalArgumentException("Unknown dialog id: " + id); } } - + class a implements OnClickListener { String mPath; EditText mDirname; @@ -398,18 +402,36 @@ public class Uploader extends ListActivity implements OnItemClickListener, andro } for (int i = 0; i < mUploadStreams.size(); ++i) { - final Cursor c = getContentResolver().query((Uri) mUploadStreams.get(i), null, null, null, null); - c.moveToFirst(); - - if (!wdc.putFile(c.getString(c.getColumnIndex(Media.DATA)), - mUploadPath+"/"+c.getString(c.getColumnIndex(Media.DISPLAY_NAME)), - c.getString(c.getColumnIndex(Media.MIME_TYPE)))) { - mHandler.post(new Runnable() { - public void run() { - Uploader.this.onUploadComplete(false, "Error while uploading file: " + c.getString(c.getColumnIndex(Media.DISPLAY_NAME))); - } - }); + Uri uri = (Uri) mUploadStreams.get(i); + if (uri.getScheme().equals("content")) { + final Cursor c = getContentResolver().query((Uri) mUploadStreams.get(i), null, null, null, null); + c.moveToFirst(); + + if (!wdc.putFile(c.getString(c.getColumnIndex(Media.DATA)), + mUploadPath+"/"+c.getString(c.getColumnIndex(Media.DISPLAY_NAME)), + c.getString(c.getColumnIndex(Media.MIME_TYPE)))) { + mHandler.post(new Runnable() { + public void run() { + Uploader.this.onUploadComplete(false, "Error while uploading file: " + c.getString(c.getColumnIndex(Media.DISPLAY_NAME))); + } + }); + } + } else if (uri.getScheme().equals("file")) { + final File file = new File(Uri.decode(uri.toString()).replace(uri.getScheme()+"://", "")); + FileNameMap fileNameMap = URLConnection.getFileNameMap(); + String contentType = fileNameMap.getContentTypeFor(uri.toString()); + if (contentType == null) { + contentType = "text/plain"; + } + if (!wdc.putFile(file.getAbsolutePath(), mUploadPath+"/"+file.getName(), contentType)) { + mHandler.post(new Runnable() { + public void run() { + Uploader.this.onUploadComplete(false, "Error while uploading file: " + file.getName()); + } + }); + } } + } mHandler.post(new Runnable() { public void run() { diff --git a/src/eu/alefzero/owncloud/WebdavClient.java b/src/eu/alefzero/owncloud/WebdavClient.java index 7ed11cee..84598037 100644 --- a/src/eu/alefzero/owncloud/WebdavClient.java +++ b/src/eu/alefzero/owncloud/WebdavClient.java @@ -1,9 +1,12 @@ package eu.alefzero.owncloud; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; +import java.io.OutputStreamWriter; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; @@ -78,10 +81,12 @@ public class WebdavClient { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { return false; } - InputStreamReader isr = new InputStreamReader(response.getEntity().getContent()); + BufferedInputStream bis = new BufferedInputStream(response.getEntity().getContent()); FileOutputStream fos = new FileOutputStream(targetPath); - int oneByte; - while ((oneByte = isr.read()) != -1) fos.write(oneByte); + + byte[] bytes = new byte[512]; + int readResult; + while ((readResult = bis.read(bytes)) != -1) fos.write(bytes, 0, readResult); } catch (IOException e) { e.printStackTrace(); @@ -124,7 +129,7 @@ public class WebdavClient { */ Log.i(TAG, "Uploading, done"); } catch (final Exception e) { - Log.i(TAG, e.getLocalizedMessage()); + Log.i(TAG, ""+e.getMessage()); result = false; }