Example usage for android.provider DocumentsContract isTreeUri

List of usage examples for android.provider DocumentsContract isTreeUri

Introduction

In this page you can find the example usage for android.provider DocumentsContract isTreeUri.

Prototype

public static boolean isTreeUri(Uri uri) 

Source Link

Document

Test if the given URI represents a Document tree.

Usage

From source file:com.github.chenxiaolong.dualbootpatcher.FileUtils.java

@NonNull
public static DocumentFile getDocumentFile(@NonNull Context context, @NonNull Uri uri) {
    DocumentFile df = null;/*  w  w w . ja va2 s.com*/

    if (FILE_SCHEME.equals(uri.getScheme())) {
        df = DocumentFile.fromFile(new File(uri.getPath()));
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && SAF_SCHEME.equals(uri.getScheme())
            && SAF_AUTHORITY.equals(uri.getAuthority())) {
        if (DocumentsContract.isDocumentUri(context, uri)) {
            df = DocumentFile.fromSingleUri(context, uri);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && DocumentsContract.isTreeUri(uri)) {
            df = DocumentFile.fromTreeUri(context, uri);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // Best guess is that it's a tree...
            df = DocumentFile.fromTreeUri(context, uri);
        }
    }

    if (df == null) {
        throw new IllegalArgumentException("Invalid URI: " + uri);
    }

    return df;
}