Class FileCtrl

java.lang.Object
com.unitech.api.file.FileCtrl

public class FileCtrl extends Object
This controller is used to read / write file to storage with system permission.
  • Field Details

  • Method Details

    • getInstance

      public static FileCtrl getInstance(Context context)
      Gets instance.
      Parameters:
      context - the context
      Returns:
      the instance
    • writeUTF8ToFile

      public Bundle writeUTF8ToFile(String path, String content)
      Write String(utf8) to file.
      Parameters:
      path - the path (String)
      content - the content (String)
      Returns:
      A bundle contains the error code and message
      int errorCode = bundle.getInt("errorCode"), (error:1, success=0)
      String errorMsg = bundle.getString("errorMsg");
    • writeToFile

      public Bundle writeToFile(String path, byte[] content)
      Write to file with byte[]
      Parameters:
      path - the path (String)
      content - the content (byte[], but if the input source only allows strings, you can use base64 to encode the byte[] into a string to process byte input, e.g. SXQncyBhIGJlYXV0aWZ1bCBkYXkgdG9kYXku)
      Returns:
      A bundle contains the error code and message
      int errorCode = bundle.getInt("errorCode"), (error:1, success=0)
      String errorMsg = bundle.getString("errorMsg");
    • readFromFile

      public Bundle readFromFile(String path)
      Read from file.
      Parameters:
      path - the path (String)
      Returns:
      A bundle contains the error code and message
      int errorCode = bundle.getInt("errorCode"), (error:1, success=0)
      String errorMsg = bundle.getString("errorMsg");
      byte[] content = bundle.getByteArray("Data");
      String base64Content = bundle.getString("Base64Data");
      String utf8Content = bundle.getString("UTF8Data") if there is
    • copyFile

      public Bundle copyFile(String fromPath, String toPath)
      Copy file bundle.
      Parameters:
      fromPath - the from path (String) e.g. /sdcard/test.txt
      toPath - the to path (String) e.g. /sdcard/Download/test.txt
      Returns:
      A bundle contains the error code and message
      int errorCode = bundle.getInt("errorCode"), (error:1, success=0)
      String errorMsg = bundle.getString("errorMsg");
    • createFile

      public Bundle createFile(String path)
      Create file or folder on device
      Parameters:
      path - The path of file or folder. If you want to create folder, the path should end with "/" like "/sdcard/newfolder/" (String)
      Returns:
      A bundle contains the error code and message
      int errorCode = bundle.getInt("errorCode"), (error:1, success=0)
      String errorMsg = bundle.getString("errorMsg");
    • deleteFile

      public Bundle deleteFile(String path)
      Deletes the file or folder. If this pathname denotes a folder, then the folder must be empty in order to be deleted.
      cannot delete folder within /Android/data or /Android/obb
      Parameters:
      path - The path of file or folder. (String)
      Returns:
      A bundle contains the error code and message
      int errorCode = bundle.getInt("errorCode"), (error:1, success=0)
      String errorMsg = bundle.getString("errorMsg");
    • createFileStream

      public Bundle createFileStream(String filepath, boolean keepContents)
      Create a stream that you can use the stream to read or write data into the file.
      If you have completed your operation on this stream, you should call FileCtrl.closeFileStream(fileStreamId) to close this stream.
      Parameters:
      filepath - The file path that you want to make a stream on it. (String)
      keepContents - Whether you want to keep the file contents. If you want to read data from the stream, you should set keepContents as true. (true: keep, false: erase file) (boolean)
      Returns:
      A bundle contains the error code and message
      int errorCode = bundle.getInt("errorCode"), (error:1, success=0)
      String errorMsg = bundle.getString("errorMsg");
      ParcelFileDescriptor pfd = bundle.getParcelable(FileCtrl.BUNDLE_PARCEL_FILE_DESCRIPTOR);
      long fileStreamId = bundle.getLong(FileCtrl.BUNDLE_FILE_STREAM_ID);
    • closeFileStream

      public Bundle closeFileStream(long fileStreamId)
      Close the specific file stream that created by FileCtrl.createFileStream.
      Parameters:
      fileStreamId - The file stream ID that you want to close. (long)
      Returns:
      A bundle contains the error code and message
      int errorCode = bundle.getInt("errorCode"), (error:1, success=0)
      String errorMsg = bundle.getString("errorMsg");
    • exists

      public Bundle exists(String path)
      Check if the file or folder exists.
      Parameters:
      path - The path of the file or folder. (String)
      Returns:
      A bundle contains the error code and message
      int errorCode = bundle.getInt("errorCode"), (1: file or folder does not exist, 0: file or folder exists)
      String errorMsg = bundle.getString("errorMsg");
    • isDirectory

      public Bundle isDirectory(String path)
      Check if path is directory.
      Parameters:
      path - The path of the file or folder. (String)
      Returns:
      A bundle contains the error code and message
      int errorCode = bundle.getInt("errorCode"), (1: path is not directory, 0: path is directory)
      String errorMsg = bundle.getString("errorMsg");
    • openFile

      public Bundle openFile(String path)
      open file for the given package name and path
      not support openFile for path start with /Android/data or /Android/obb
      Parameters:
      path - The path of the file. (String)
      Returns:
      A bundle contains the error code and message
      int errorCode = bundle.getInt("errorCode"), (1: Cannot open the file, 0: success to open the file)
      String errorMsg = bundle.getString("errorMsg");
    • list

      public Bundle list(String path)
      Returns an array of strings naming the files and directories in the
      directory denoted by this abstract pathname.
      not support for path start with /Android/data or /Android/obb
      Parameters:
      path - The path of the file or folder. (String)
      Returns:
      A bundle contains the error code and message
      int errorCode = bundle.getInt("errorCode"), (1: list is null, 0: list is not null)
      String errorMsg = bundle.getString("errorMsg");
    • renameTo

      public Bundle renameTo(String path, String newName)
      Renames the file denoted by this name.
      not support for path start with /Android/data or /Android/obb
      Parameters:
      path - The path of the file or folder. (String)
      Returns:
      A bundle contains the error code and message
      int errorCode = bundle.getInt("errorCode"), (1: fail, 0: success)
      String errorMsg = bundle.getString("errorMsg");
    • canWrite

      public Bundle canWrite(String path)
      Tests whether the application can modify the file denoted by this abstract pathname.
      not support canWrite for path start with /Android/data or /Android/obb
      Parameters:
      path - The path of the file or folder. (String)
      Returns:
      A bundle contains the error code and message
      int errorCode = bundle.getInt("errorCode"), (1: fail, 0: success)
      String errorMsg = bundle.getString("errorMsg");
    • canRead

      public Bundle canRead(String path)
      Tests whether the application can read the file denoted by this abstract pathname
      not support canRead for path start with /Android/data or /Android/obb
      Parameters:
      path - The path of the file or folder. (String)
      Returns:
      A bundle contains the error code and message
      int errorCode = bundle.getInt("errorCode"), (1: fail, 0: success)
      String errorMsg = bundle.getString("errorMsg");
    • getExternalStorageInfo

      public Bundle getExternalStorageInfo()
      get device external storage information include real path and description
      Returns:
      A bundle contains the error code and message
      int errorCode = bundle.getInt("errorCode"), (1: fail, 0: success)
      String errorMsg = bundle.getString("errorMsg");
    • pathConverter

      public Bundle pathConverter(String path)
      get real storage path description
      Parameters:
      path - The path of the file or folder that start with USB/, SD/ or FLASH/ (String)
      Returns:
      String indicate the real storage path
      use bundle.getString("REAL_PATH") to get converted path int errorCode = bundle.getInt("errorCode"), (1: fail, 0: success)
      String errorMsg = bundle.getString("errorMsg");