android.provider.DocumentsContract#getTreeDocumentId ( )源码实例Demo

下面列出了android.provider.DocumentsContract#getTreeDocumentId ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: UniFile   文件: DocumentsContractApi21.java
public static Uri prepareTreeUri(Uri treeUri) {
    String documentId;
    try {
        documentId = DocumentsContract.getDocumentId(treeUri);
        if (documentId == null) {
            throw new IllegalArgumentException();
        }
    } catch (Exception e) {
        // IllegalArgumentException will be raised
        // if DocumentsContract.getDocumentId() failed.
        // But it isn't mentioned the document,
        // catch all kinds of Exception for safety.
        documentId = DocumentsContract.getTreeDocumentId(treeUri);
    }
    return DocumentsContract.buildDocumentUriUsingTree(treeUri, documentId);
}
 
源代码2 项目: CommonUtils   文件: FileUtils.java
@Nullable
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static String getVolumeIdFromTreeUri(@NonNull Uri treeUri) {
    final String docId = DocumentsContract.getTreeDocumentId(treeUri);
    final String[] split = docId.split(":");
    if (split.length > 0) return split[0];
    else return null;
}
 
源代码3 项目: CommonUtils   文件: FileUtils.java
@NonNull
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static String getDocumentPathFromTreeUri(@NonNull Uri treeUri) {
    final String docId = DocumentsContract.getTreeDocumentId(treeUri);
    final String[] split = docId.split(":");
    if ((split.length >= 2) && (split[1] != null)) return split[1];
    else return File.separator;
}
 
源代码4 项目: PowerFileExplorer   文件: AndroidPathUtils.java
/**
 * Get the volume ID from the tree URI.
 *
 * @param treeUri The tree URI.
 * @return The volume ID.
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static String getVolumeIdFromTreeUri(final Uri treeUri) {
	final String docId = DocumentsContract.getTreeDocumentId(treeUri);
	final String[] split = docId.split(":");

	if (split.length > 0) {
		return split[0];
	} else {
		return null;
	}
}
 
源代码5 项目: PowerFileExplorer   文件: AndroidPathUtils.java
/**
 * Get the document path (relative to volume name) for a tree URI (LOLLIPOP).
 *
 * @param treeUri The tree URI.
 * @return the document path.
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static String getDocumentPathFromTreeUri(final Uri treeUri) {
	final String docId = DocumentsContract.getTreeDocumentId(treeUri);
	final String[] split = docId.split(":");
	if ((split.length >= 2) && (split[1] != null)) {
		return split[1];
	} else {
		return File.separator;
	}
}
 
源代码6 项目: SAI   文件: SafUtils.java
public static String getRootForPath(Uri docUri) {
    String path = DocumentsContract.getTreeDocumentId(docUri);

    int indexOfLastColon = path.lastIndexOf(':');
    if (indexOfLastColon == -1)
        throw new IllegalArgumentException("Given uri does not contain a colon: " + docUri);

    return path.substring(0, indexOfLastColon);
}
 
源代码7 项目: SAI   文件: SafUtils.java
public static String getPathWithoutRoot(Uri docUri) {
    String path = DocumentsContract.getTreeDocumentId(docUri);

    int indexOfLastColon = path.lastIndexOf(':');
    if (indexOfLastColon == -1)
        throw new IllegalArgumentException("Given uri does not contain a colon: " + docUri);

    return path.substring(indexOfLastColon + 1);
}
 
源代码8 项目: edslite   文件: DocumentTreeLocation.java
public static boolean isDocumentTreeUri(Context context, Uri uri)
{
	try
	{
		//noinspection ConstantConditions
		return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP &&
				DocumentsContract.getTreeDocumentId(uri) != null && DocumentFile.isDocumentUri(context, uri);
	}
	catch (IllegalArgumentException e)
	{
		return false;
	}
}
 
源代码9 项目: Hentoid   文件: FileUtil.java
/**
 * WARNING This is a tweak of internal Android code to make it faster by caching calls to queryIntentContentProviders
 * Original (uncached) is DocumentFile.fromTreeUri
 */
@Nullable
private static DocumentFile fromTreeUriCached(@NonNull final Context context, @NonNull final Uri treeUri) {
    String documentId = DocumentsContract.getTreeDocumentId(treeUri);
    if (isDocumentUriCached(context, treeUri)) {
        documentId = DocumentsContract.getDocumentId(treeUri);
    }
    return newTreeDocumentFile(null, context,
            DocumentsContract.buildDocumentUriUsingTree(treeUri,
                    documentId));
}
 
源代码10 项目: Hentoid   文件: FileHelper.java
private static String getVolumeIdFromUri(final Uri uri, boolean isFolder) {
    final String docId;
    if (isFolder) docId = DocumentsContract.getTreeDocumentId(uri);
    else docId = DocumentsContract.getDocumentId(uri);

    final String[] split = docId.split(":");
    if (split.length > 0) return split[0];
    else return null;
}
 
源代码11 项目: Hentoid   文件: FileHelper.java
private static String getDocumentPathFromUri(final Uri uri, boolean isFolder) {
    final String docId;
    if (isFolder) docId = DocumentsContract.getTreeDocumentId(uri);
    else docId = DocumentsContract.getDocumentId(uri);

    final String[] split = docId.split(":");
    if ((split.length >= 2) && (split[1] != null)) return split[1];
    else return File.separator;
}
 
源代码12 项目: Hentoid   文件: FileHelper.java
public static void revokePreviousPermissions(@NonNull final ContentResolver resolver, @NonNull final Uri newUri) {
    // Unfortunately, the content Uri of the selected resource is not exactly the same as the one stored by ContentResolver
    // -> solution is to compare their TreeDocumentId instead
    String treeUriId = DocumentsContract.getTreeDocumentId(newUri);

    for (UriPermission p : resolver.getPersistedUriPermissions())
        if (!DocumentsContract.getTreeDocumentId(p.getUri()).equals(treeUriId))
            resolver.releasePersistableUriPermission(p.getUri(),
                    Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    if (resolver.getPersistedUriPermissions().isEmpty()) {
        Timber.d("Permissions revoked successfully.");
    } else {
        Timber.d("Permissions failed to be revoked.");
    }
}
 
源代码13 项目: fdroidclient   文件: TreeUriUtils.java
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static String getVolumeIdFromTreeUri(final Uri treeUri) {
    final String docId = DocumentsContract.getTreeDocumentId(treeUri);
    final String[] split = docId.split(":");
    if (split.length > 0) return split[0];
    else return null;
}
 
源代码14 项目: fdroidclient   文件: TreeUriUtils.java
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static String getDocumentPathFromTreeUri(final Uri treeUri) {
    final String docId = DocumentsContract.getTreeDocumentId(treeUri);
    final String[] split = docId.split(":");
    if ((split.length >= 2) && (split[1] != null)) return split[1];
    else return File.separator;
}
 
源代码15 项目: syncthing-android   文件: FileUtils.java
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static String getVolumeIdFromTreeUri(final Uri treeUri) {
    final String docId = DocumentsContract.getTreeDocumentId(treeUri);
    final String[] split = docId.split(":");
    if (split.length > 0) {
        return split[0];
    } else {
        return null;
    }
}
 
源代码16 项目: syncthing-android   文件: FileUtils.java
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static String getDocumentPathFromTreeUri(final Uri treeUri) {
    final String docId = DocumentsContract.getTreeDocumentId(treeUri);
    final String[] split = docId.split(":");
    if ((split.length >= 2) && (split[1] != null)) return split[1];
    else return File.separator;
}
 
源代码17 项目: Augendiagnose   文件: FileUtil.java
/**
 * Get the volume ID from the tree URI.
 *
 * @param treeUri The tree URI.
 * @return The volume ID.
 */
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private static String getVolumeIdFromTreeUri(final Uri treeUri) {
	final String docId = DocumentsContract.getTreeDocumentId(treeUri);
	final String[] split = docId.split(":");

	if (split.length > 0) {
		return split[0];
	}
	else {
		return null;
	}
}
 
源代码18 项目: Augendiagnose   文件: FileUtil.java
/**
 * Get the document path (relative to volume name) for a tree URI (LOLLIPOP).
 *
 * @param treeUri The tree URI.
 * @return the document path.
 */
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private static String getDocumentPathFromTreeUri(final Uri treeUri) {
	final String docId = DocumentsContract.getTreeDocumentId(treeUri);
	final String[] split = docId.split(":");
	if ((split.length >= 2) && (split[1] != null)) {
		return split[1];
	}
	else {
		return File.separator;
	}
}
 
源代码19 项目: Hentoid   文件: Api29MigrationActivity.java
public void onSelectSAFRootFolder(@NonNull final Uri treeUri) {

        boolean isUriPermissionPeristed = false;
        ContentResolver contentResolver = getContentResolver();
        String treeUriId = DocumentsContract.getTreeDocumentId(treeUri);

        for (UriPermission p : contentResolver.getPersistedUriPermissions()) {
            if (DocumentsContract.getTreeDocumentId(p.getUri()).equals(treeUriId)) {
                isUriPermissionPeristed = true;
                Timber.d("Uri permission already persisted for %s", treeUri);
                break;
            }
        }

        if (!isUriPermissionPeristed) {
            Timber.d("Persisting Uri permission for %s", treeUri);
            // Release previous access permissions, if different than the new one
            FileHelper.revokePreviousPermissions(contentResolver, treeUri);
            // Persist new access permission
            contentResolver.takePersistableUriPermission(treeUri,
                    Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        }

        DocumentFile selectedFolder = DocumentFile.fromTreeUri(this, treeUri);
        if (selectedFolder != null) {
            String folderName = selectedFolder.getName();
            if (null == folderName) folderName = "";

            // Make sure we detect the Hentoid folder if it's a child of the selected folder
            if (!ImportHelper.isHentoidFolderName(folderName))
                selectedFolder = ImportHelper.getExistingHentoidDirFrom(this, selectedFolder);
        }

        // If no existing hentoid folder is detected, tell the user to select it again
        if (null == selectedFolder || null == selectedFolder.getName() || !ImportHelper.isHentoidFolderName(selectedFolder.getName()))
        {
            ToastUtil.toast("Please select an existing Hentoid folder. Its location is displayed on screen.");
            return;
        }
        scanLibrary(selectedFolder);
    }
 
源代码20 项目: Hentoid   文件: ImportHelper.java
public static @Result
int setAndScanFolder(
        @NonNull final Context context,
        @NonNull final Uri treeUri,
        boolean askScanExisting,
        @Nullable final ImportOptions options) {

    boolean isUriPermissionPeristed = false;
    ContentResolver contentResolver = context.getContentResolver();
    String treeUriId = DocumentsContract.getTreeDocumentId(treeUri);

    for (UriPermission p : contentResolver.getPersistedUriPermissions()) {
        if (DocumentsContract.getTreeDocumentId(p.getUri()).equals(treeUriId)) {
            isUriPermissionPeristed = true;
            Timber.d("Uri permission already persisted for %s", treeUri);
            break;
        }
    }

    if (!isUriPermissionPeristed) {
        Timber.d("Persisting Uri permission for %s", treeUri);
        // Release previous access permissions, if different than the new one
        FileHelper.revokePreviousPermissions(contentResolver, treeUri);
        // Persist new access permission
        contentResolver.takePersistableUriPermission(treeUri,
                Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    }

    DocumentFile docFile = DocumentFile.fromTreeUri(context, treeUri);
    if (null == docFile || !docFile.exists()) {
        Timber.e("Could not find the selected file %s", treeUri.toString());
        return Result.INVALID_FOLDER;
    }
    DocumentFile hentoidFolder = addHentoidFolder(context, docFile);
    if (null == hentoidFolder) {
        Timber.e("Could not create Hentoid folder in root %s", docFile.getUri().toString());
        return Result.CREATE_FAIL;
    }
    if (!FileHelper.checkAndSetRootFolder(context, hentoidFolder, true)) {
        Timber.e("Could not set the selected root folder %s", hentoidFolder.getUri().toString());
        return Result.INVALID_FOLDER;
    }

    if (hasBooks(context)) {
        if (!askScanExisting) {
            runImport(context, options);
            return Result.OK_LIBRARY_DETECTED;
        } else return Result.OK_LIBRARY_DETECTED_ASK;
    } else {
        // New library created - drop and recreate db (in case user is re-importing)
        new ObjectBoxDAO(context).deleteAllLibraryBooks(true);
        return Result.OK_EMPTY_FOLDER;
    }
}