Java Code Examples for android.support.v4.provider.DocumentFile#createFile()

The following examples show how to use android.support.v4.provider.DocumentFile#createFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OTGUtil.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Traverse to a specified path in OTG
 *
 * @param path
 * @param context
 * @param createRecursive flag used to determine whether to create new file while traversing to path,
 *                        in case path is not present. Notably useful in opening an output stream.
 * @return
 */
public static DocumentFile getDocumentFile(String path, Context context, boolean createRecursive) {
    SharedPreferences manager = PreferenceManager.getDefaultSharedPreferences(context);
    String rootUriString = manager.getString(ExplorerActivity.KEY_PREF_OTG, null);

    // start with root of SD card and then parse through document tree.
    DocumentFile rootUri = DocumentFile.fromTreeUri(context, Uri.parse(rootUriString));

    String[] parts = path.split("/");
    for (int i = 0; i < parts.length; i++) {

        if (path.equals("otg:/")) break;
        if (parts[i].equals("otg:") || parts[i].equals("")) continue;
        Log.d(context.getClass().getSimpleName(), "Currently at: " + parts[i]);
        // iterating through the required path to find the end point

        DocumentFile nextDocument = rootUri.findFile(parts[i]);
        if (createRecursive) {
            if (nextDocument == null || !nextDocument.exists()) {
                nextDocument = rootUri.createFile(parts[i].substring(parts[i].lastIndexOf(".")), parts[i]);
                Log.d(context.getClass().getSimpleName(), "NOT FOUND! File created: " + parts[i]);
            }
        }
        rootUri = nextDocument;
    }
    return rootUri;
}
 
Example 2
Source File: OutputUtil.java    From apkextractor with GNU General Public License v3.0 5 votes vote down vote up
public static @Nullable DocumentFile getWritingDocumentFileForAppItem(@NonNull Context context,@NonNull AppItem appItem,@NonNull String extension,int sequence_number) throws Exception{
    String writingFileName=getWriteFileNameForAppItem(context,appItem,extension,sequence_number);
    DocumentFile parent=getExportPathDocumentFile(context);
    DocumentFile documentFile=parent.findFile(writingFileName);
    if(documentFile!=null&&documentFile.exists())documentFile.delete();
    return parent.createFile(extension.toLowerCase().equals("apk")?"application/vnd.android.package-archive":"application/x-zip-compressed",writingFileName);
}
 
Example 3
Source File: StorageUtil.java    From Camera-Roll-Android-App with Apache License 2.0 5 votes vote down vote up
public static DocumentFile createDocumentFile(Context context, Uri treeUri, String path, String mimeType) {
    int index = path.lastIndexOf("/");
    String dirPath = path.substring(0, index);
    DocumentFile file = parseDocumentFile(context, treeUri, new File(dirPath));
    if (file != null) {
        String name = path.substring(index + 1);
        file = file.createFile(mimeType, name);
    }
    return file;
}
 
Example 4
Source File: FileUtil.java    From PowerFileExplorer with GNU General Public License v3.0 4 votes vote down vote up
/**
   * Get a DocumentFile corresponding to the given file (for writing on ExtSdCard on Android 5). If the file is not
   * existing, it is created.
   *
   * @param file        The file.
   * @param isDirectory flag indicating if the file should be a directory.
   * @return The DocumentFile
   */
  public static DocumentFile getDocumentFile(final File file, final boolean isDirectory, Context context) {
      final String baseFolder = getExtSdCardFolder(file, context);
      boolean originalDirectory = false;
      if (baseFolder == null) {
          return null;
      }

      String relativePath = null;
      try {
          final String fullPath = file.getCanonicalPath();
          if (!baseFolder.equals(fullPath))
              relativePath = fullPath.substring(baseFolder.length() + 1);
          else 
		originalDirectory = true;
      } catch (IOException e) {
          return null;
      } catch (Exception f) {
          originalDirectory = true;
          //continue
      }
      final String as = PreferenceManager.getDefaultSharedPreferences(context).getString("URI", null);

      Uri treeUri = null;
      if (as != null) 
	treeUri = Uri.parse(as);
      if (treeUri == null) {
          return null;
      }

      // start with root of SD card and then parse through document tree.
      DocumentFile document = DocumentFile.fromTreeUri(context, treeUri);
if (originalDirectory) 
	return document;
      final String[] parts = relativePath.split("\\/");
//Log.d("FileUtil", "relativePath " + relativePath);
      for (int i = 0; i < parts.length; i++) {
          //Log.d("FileUtil", "parts[] " + parts[i]);
	if (document == null) {
		return null;
	}
	DocumentFile nextDocument = document.findFile(parts[i]);

          if (nextDocument == null) {
              if ((i < parts.length - 1) || isDirectory) {
                  nextDocument = document.createDirectory(parts[i]);
              } else {
                  nextDocument = document.createFile("image", parts[i]);
              }
          }
          //Log.d("FileUtil", "nextDocument " + nextDocument);
	document = nextDocument;
      }

      return document;
  }
 
Example 5
Source File: AndroidPathUtils.java    From PowerFileExplorer with GNU General Public License v3.0 4 votes vote down vote up
/**
	 * Get a DocumentFile corresponding to the given file (for writing on ExtSdCard on Android 5). If the file is not
	 * existing, it is created.
	 *
	 * @param file              The file.
	 * @param isDirectory       flag indicating if the file should be a directory.
	 * @param createDirectories flag indicating if intermediate path directories should be created if not existing.
	 * @return The DocumentFile
	 */
	public static DocumentFile getDocumentFile(@NonNull final File file, final boolean isDirectory,
											   final boolean createDirectories, final Context c) {
//		Log.d("getDocumentFile start", df.format(System.currentTimeMillis()));
		String baseFolder = null;
		baseFolder = getExtSdCardFolder(file, c);
		if (baseFolder == null) {
			return null;
		}
		String relativePath;
		try {
			String fullPath = file.getCanonicalPath();
			relativePath = fullPath.substring(baseFolder.length() + 1);
		} catch (IOException e) {
			return null;
		}

		if (treeUri == null) {
			treeUri = getSharedPreferenceUri("key_internal_uri_extsdcard", c);
			if (treeUri == null) { 
				return null; 
			} 
		}

		// start with root of SD card and then parse through document tree.
		DocumentFile document = DocumentFile.fromTreeUri(c, treeUri);

		String[] parts = relativePath.split("\\/");
		for (int i = 0; i < parts.length; i++) {
			DocumentFile nextDocument = document.findFile(parts[i]);

			if (nextDocument == null) {
				if (i < parts.length - 1) {
					if (createDirectories) {
						nextDocument = document.createDirectory(parts[i]);
					} else {
						return null;
					}
				} else if (isDirectory) {
					nextDocument = document.createDirectory(parts[i]);
				} else {
					nextDocument = document.createFile("image", parts[i]);
				}
			}
			document = nextDocument;
		}
//		Log.d("getDocumentFile end", df.format(System.currentTimeMillis()));
		return document;
	}