Brad Bass
Brad Bass

Reputation: 371

Transfer file from phone to network share

First off, thanks in advance for any help you can provide.

My issue is, hopefully, one that can be resolved. I have an app that, basically, allows a user to input data and then sends that data via email as an attachment. What I would like to do, is if the user is connected to their network via wifi, that instead of sending the file via email, it would transfer the file to a network share. I have been searching for an answer for quite a while but unfortunately have found no way of doing this.

So I guess my real question is if this is even possible, and if so, how to go about doing this.

Upvotes: 0

Views: 158

Answers (1)

Aaron McIver
Aaron McIver

Reputation: 24713

You would need to copy the file accordingly as noted below and in your instance I would presume the dest File would get set up as such...

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);
  }
}

Upvotes: 1

Related Questions