2013-06-19 65 views
0

首先,感謝您提供的任何幫助。將文件從手機傳輸到網絡共享

我的問題是,希望可以解決。我有一個應用程序,基本上允許用戶輸入數據,然後通過電子郵件作爲附件發送該數據。我想要做的是,如果用戶通過WiFi連接到他們的網絡,而不是通過電子郵件發送文件,它會將文件傳輸到網絡共享。我一直在尋找一個答案很長一段時間,但不幸的是沒有找到辦法做到這一點。

所以我想我真正的問題是如果這是可能的,如果是這樣,如何去做這件事。

+0

的可能重複[安卓:檢查通過3G或WiFi網絡打開或可用或不是在Android設備](http://stackoverflow.com/questions/11343249/android-check-3g-or-wifi-network-is-on-or-available-or-not-on-android-設備) –

+0

不,那不是問題。我沒有問題檢查用戶是否連接到無線網絡。問題是我們知道用戶連接到無線網絡後,如何訪問網絡共享。謝謝。 –

回答

1

如下,並在您的例子中,我推測這個DEST File將得到建立這樣指出你會需要相應copy the file ...

new File("\\\\server\\path\\to\\file.txt") 

class FileUtils { 
    public static boolean copyFile(File source, File dest) { 
    BufferedInputStream bis = null; 
    BufferedOutputStream bos = null; 

    try { 
     bis = new BufferedInputStream(new FileInputStream(source)); 
     bos = new BufferedOutputStream(new FileOutputStream(dest, false)); 

     byte[] buf = new byte[1024]; 
     bis.read(buf); 

     do { 
     bos.write(buf); 
     } while(bis.read(buf) != -1); 
    } catch (IOException e) { 
     return false; 
    } finally { 
     try { 
     if (bis != null) bis.close(); 
     if (bos != null) bos.close(); 
     } catch (IOException e) { 
     return false; 
     } 
    } 

    return true; 
    } 

    // WARNING ! Inefficient if source and dest are on the same filesystem ! 
    public static boolean moveFile(File source, File dest) { 
    return copyFile(source, dest) && source.delete(); 
    } 

    // Returns true if the sdcard is mounted rw 
    public static boolean isSDMounted() { 
    return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); 
    } 
} 
+0

謝謝亞倫。比我想象的要簡單得多。我擔心的是我無法「看到」網絡共享。再次感謝 :) –

相關問題