downloading file
authorBartek Przybylski <bart.p.pl@gmail.com>
Sun, 25 Dec 2011 12:04:18 +0000 (13:04 +0100)
committerBartek Przybylski <bart.p.pl@gmail.com>
Sun, 25 Dec 2011 12:04:18 +0000 (13:04 +0100)
src/eu/alefzero/owncloud/FileDownloader.java
src/eu/alefzero/owncloud/OwnCloudMainScreen.java
src/eu/alefzero/owncloud/Uploader.java
src/eu/alefzero/owncloud/WebdavClient.java

index 0e8e03b..0c55044 100644 (file)
@@ -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);
     
index 65592b1..c89672c 100644 (file)
@@ -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();
-      }
+
     }
   }
   
index 66dec56..5a6a4a2 100644 (file)
@@ -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() {
index 7ed11ce..8459803 100644 (file)
@@ -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;
     }