本文整理汇总了Java中android.provider.DocumentsContract.buildDocumentUriUsingTree方法的典型用法代码示例。如果您正苦于以下问题:Java DocumentsContract.buildDocumentUriUsingTree方法的具体用法?Java DocumentsContract.buildDocumentUriUsingTree怎么用?Java DocumentsContract.buildDocumentUriUsingTree使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.provider.DocumentsContract
的用法示例。
在下文中一共展示了DocumentsContract.buildDocumentUriUsingTree方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: listFiles
import android.provider.DocumentsContract; //导入方法依赖的package包/类
public static Uri[] listFiles(Context context, Uri self) {
final ContentResolver resolver = context.getContentResolver();
final Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(self,
DocumentsContract.getDocumentId(self));
final ArrayList<Uri> results = new ArrayList<Uri>();
Cursor c = null;
try {
c = resolver.query(childrenUri, new String[] {
DocumentsContract.Document.COLUMN_DOCUMENT_ID }, null, null, null);
while (c.moveToNext()) {
final String documentId = c.getString(0);
final Uri documentUri = DocumentsContract.buildDocumentUriUsingTree(self,
documentId);
results.add(documentUri);
}
} catch (Exception e) {
Log.w(TAG, "Failed query: " + e);
} finally {
closeQuietly(c);
}
return results.toArray(new Uri[results.size()]);
}
示例2: setUri
import android.provider.DocumentsContract; //导入方法依赖的package包/类
@Override
public void setUri(Uri uri_)
{
if (this.uri == null && isTreeUri(uri_))
{
try
{
ctx.getContentResolver().takePersistableUriPermission(uri_,
Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
catch (Exception e)
{
e.printStackTrace();
}
this.uri = DocumentsContract.buildDocumentUriUsingTree(uri_, DocumentsContract.getTreeDocumentId(uri_));
}
else
{
this.uri = uri_;
}
}
示例3: buildTreeFile
import android.provider.DocumentsContract; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static NoobFile buildTreeFile(Activity activity, Uri uri) throws SecurityException {
if (uri == null)
return null;
ContentResolver contentResolver = activity.getContentResolver();
Uri docUri = DocumentsContract.buildDocumentUriUsingTree(uri,
DocumentsContract.getTreeDocumentId(uri));
Cursor docCursor = contentResolver.query(docUri, new String[]{
DocumentsContract.Document.COLUMN_DISPLAY_NAME, DocumentsContract.Document.COLUMN_MIME_TYPE, DocumentsContract.Document.COLUMN_DOCUMENT_ID}, null, null, null);
if (docCursor != null && docCursor.moveToNext()) {
Log.d(TAG, "found doc =" + docCursor.getString(0) + ", mime=" + docCursor
.getString(1));
NoobFile file = new NoobFile(activity, docCursor, uri, true);
docCursor.close();
return file;
}
return null;
}
示例4: onActivityResult
import android.provider.DocumentsContract; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected void onActivityResult(final int requestCode, int resultCode, final Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
// Grab the selected permission so we can generate valid test files paths
case REQUEST_CODE_WRITE_PERMISSION:
if (resultCode == RESULT_OK && data != null)
{
Snackbar.make(findViewById(android.R.id.content), data.getDataString(), Snackbar.LENGTH_SHORT).show();
String root = DocumentsContract.getTreeDocumentId(data.getData());
test1Uri = DocumentsContract.buildDocumentUriUsingTree(data.getData(), root + documentId1);
test2Uri = DocumentsContract.buildDocumentUriUsingTree(data.getData(), root + documentId2);
}
break;
}
}
示例5: getNeighborUri
import android.provider.DocumentsContract; //导入方法依赖的package包/类
/**
* Returns a uri to a neighbor file within the same folder. This can be used to get an assumed uri
* to a neighbor within a folder. This avoids heavy calls to DocumentFile.listFiles or
* write-locked createFile
*
* This will only work with a uri that is an hierarchical tree similar to SCHEME_FILE
* @param hierarchicalTreeUri folder to install into
* @param filename filename of child file
* @return Uri to the child file
*/
@Nullable
public static Uri getNeighborUri(@NonNull Uri hierarchicalTreeUri, String filename)
{
String documentId = getDocumentId(hierarchicalTreeUri);
if (documentId == null)
return null;
String root = getRoot(documentId);
if (root == null)
return null;
String[] parts = getPathSegments(documentId);
if (parts == null)
return null;
parts[parts.length-1] = filename; // replace the filename
String path = TextUtils.join("/", parts);
String neighborId = createNewDocumentId(root, path);
return DocumentsContract.buildDocumentUriUsingTree(hierarchicalTreeUri, neighborId);
}
示例6: prepareTreeUri
import android.provider.DocumentsContract; //导入方法依赖的package包/类
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);
}
示例7: listFiles
import android.provider.DocumentsContract; //导入方法依赖的package包/类
public static Uri[] listFiles(Context context, Uri self) {
final ContentResolver resolver = context.getContentResolver();
final Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(self,
DocumentsContract.getDocumentId(self));
final ArrayList<Uri> results = new ArrayList<>();
Cursor c = null;
try {
c = resolver.query(childrenUri, new String[] {
DocumentsContract.Document.COLUMN_DOCUMENT_ID }, null, null, null);
if (null != c) {
while (c.moveToNext()) {
final String documentId = c.getString(0);
final Uri documentUri = DocumentsContract.buildDocumentUriUsingTree(self,
documentId);
results.add(documentUri);
}
}
} catch (Exception e) {
// Log.w(TAG, "Failed query: " + e);
} finally {
closeQuietly(c);
}
return results.toArray(new Uri[results.size()]);
}
示例8: getDocumentUri
import android.provider.DocumentsContract; //导入方法依赖的package包/类
/**
* For API >= 21 the file path should be translated
* from original full path: /storage/sdcard/DCIM/Camera/20150325_193246.jpg
* to a valid API Uri: content://com.android.externalstorage.documents/tree/17EA-1C19:DCIM/Camera/20150325_193246.jpg
*
* @param fullFilePath Original full path.
* @return New URI compatible file path string.
*/
@TargetApi(21)
public Uri getDocumentUri(List<SelectedFolderModel> selectedFolders, String fullFilePath) {
if (!Utilities.isEmpty(selectedFolders)) {
String currentFullPath, documentId = null, previousFullPath = "";
Uri selectedUri = null;
for (SelectedFolderModel selectedFolder : selectedFolders) {
currentFullPath = selectedFolder.getFullPath();
if (folderMatching(previousFullPath, currentFullPath, fullFilePath)) {
previousFullPath = currentFullPath;
documentId = fullFilePath.replace(selectedFolder.getRootPath() + "/", selectedFolder.getUuid() + ":");
selectedUri = selectedFolder.getUri();
}
}
if (selectedUri != null && documentId != null) {
return DocumentsContract.buildDocumentUriUsingTree(selectedUri, documentId);
}
}
return null;
}
示例9: findChildDocument
import android.provider.DocumentsContract; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static Uri findChildDocument(ContentResolver contentResolver, Uri uri, String displayName) {
String[] projection = {DocumentsContract.Document.COLUMN_DOCUMENT_ID,
DocumentsContract.Document.COLUMN_DISPLAY_NAME};
Uri childUri = DocumentsContract.buildChildDocumentsUriUsingTree(uri,
DocumentsContract.getDocumentId(uri));
Cursor cursor = contentResolver.query(childUri, projection, null, null, null);
try {
while (cursor.moveToNext()) {
if (displayName.equals(cursor.getString(1))) {
return DocumentsContract.buildDocumentUriUsingTree(uri, cursor.getString(0));
}
}
} finally {
cursor.close();
}
return null;
}
示例10: getDisplayNameForTreeUri
import android.provider.DocumentsContract; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private String getDisplayNameForTreeUri(Uri treeUri) {
Uri documentUri = DocumentsContract.buildDocumentUriUsingTree(treeUri,
DocumentsContract.getTreeDocumentId(treeUri));
try (Cursor data = getContentResolver().query(documentUri,
new String[] { DocumentsContract.Document.COLUMN_DISPLAY_NAME },
null, null, null)) {
String displayName = null;
if (data != null && data.moveToNext()) {
displayName = data.getString(data.getColumnIndex(DocumentsContract.Document.COLUMN_DISPLAY_NAME));
}
return displayName;
} catch (SecurityException e) {
// No longer can read this URI, which means no display name for this URI
return null;
}
}
示例11: NoobFile
import android.provider.DocumentsContract; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public NoobFile(Context contextParam, Cursor cursorParam, Uri treeUri, boolean isTreeDocParam) {
isTreeDoc = isTreeDocParam;
//mName = cursorParam.getString(0);
mMimeType = cursorParam.getString(1);
mDocId = cursorParam.getString(2);
isDirectory = mMimeType.equalsIgnoreCase(DocumentsContract.Document.MIME_TYPE_DIR);
if (isTreeDoc) {
mUri = treeUri;
mDocumentFile = DocumentFile.fromTreeUri(contextParam, treeUri);
} else {
mUri = DocumentsContract.buildDocumentUriUsingTree(treeUri, mDocId);
if (isDirectory) {
mDocumentFile = DocumentFile.fromTreeUri(contextParam, mUri);
} else {
mDocumentFile = DocumentFile.fromSingleUri(contextParam, mUri);
}
}
/*if (mDocumentFile.exists()) {
Log.d("DocumentENtry", "Exists true " + mName);
} else {
Log.d("DocumentENtry", "Exists false " + mName);
}*/
mName = NoobSAFManager.getValidName(mDocumentFile, isTreeDoc());
}
示例12: prepareTreeUri
import android.provider.DocumentsContract; //导入方法依赖的package包/类
public static Uri prepareTreeUri(Uri treeUri) {
return DocumentsContract.buildDocumentUriUsingTree(treeUri,
DocumentsContract.getTreeDocumentId(treeUri));
}
示例13: prepareTreeUri
import android.provider.DocumentsContract; //导入方法依赖的package包/类
public static Uri prepareTreeUri(Uri treeUri) {
return DocumentsContract.buildDocumentUriUsingTree(treeUri, DocumentsContract.getTreeDocumentId(treeUri));
}
示例14: getChildren
import android.provider.DocumentsContract; //导入方法依赖的package包/类
public final static ArrayList<SAFItem> getChildren(Context ctx, Uri u)
{
Cursor c = null;
try
{
ContentResolver cr = ctx.getContentResolver();
String document_id = DocumentsContract.getDocumentId(u);
Uri children_uri = DocumentsContract.buildChildDocumentsUriUsingTree(u, document_id);
c = cr.query(children_uri, projection, null, null, null);
if (c != null)
{
ArrayList<SAFItem> tmp_list = new ArrayList<SAFItem>();
if (c.getCount() == 0)
{
return tmp_list;
}
int ici = c.getColumnIndex(Document.COLUMN_DOCUMENT_ID);
int nci = c.getColumnIndex(Document.COLUMN_DISPLAY_NAME);
int sci = c.getColumnIndex(Document.COLUMN_SIZE);
int mci = c.getColumnIndex(Document.COLUMN_MIME_TYPE);
int dci = c.getColumnIndex(Document.COLUMN_LAST_MODIFIED);
c.moveToFirst();
do
{
SAFItem item = new SAFItem();
String id = c.getString(ici);
item.origin = DocumentsContract.buildDocumentUriUsingTree(u, id);
item.attr = c.getString(mci);
item.dir = Document.MIME_TYPE_DIR.equals(item.attr);
item.name = c.getString(nci);
item.size = c.getLong(sci);
item.date = new Date(c.getLong(dci));
if (item.dir)
{
item.size = -1;
item.attr = Integer.toString(getDirItemsNumber(ctx, (Uri) item.origin)) + " "
+ ctx.getString(R.string.dialog_list_items);
}
tmp_list.add(item);
}
while (c.moveToNext());
return tmp_list;
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (c != null)
{
c.close();
}
}
return null;
}
示例15: getParentDocument
import android.provider.DocumentsContract; //导入方法依赖的package包/类
@Nullable
protected UsefulDocumentFile getParentDocument()
{
if (FileUtil.isFileScheme(mUri))
{
File f = new File(mUri.getPath());
File parent = f.getParentFile();
if (parent == null)
return null;
return new UsefulDocumentFile(null, mContext, Uri.fromFile(parent));
}
String documentId = getDocumentId();
String[] parts = DocumentUtil.getPathSegments(documentId);
if (parts == null)
{
// Might be a root, try to get the tree uri
try
{
String treeId = DocumentsContract.getTreeDocumentId(mUri);
return UsefulDocumentFile.fromUri(mContext, DocumentsContract.buildTreeDocumentUri(mUri.getAuthority(), treeId));
}
catch (IllegalArgumentException e)
{
return null;
}
}
String[] parentParts = Arrays.copyOfRange(parts, 0, parts.length - 1);
String path = TextUtils.join("/", parentParts);
String root = DocumentUtil.getRoot(documentId);
String parentId = DocumentUtil.createNewDocumentId(root, path);
Uri parentUri;
/** Removed isDocumentUri check because it involves a resolver check (slow)
* and the benefits of blindly creating a treeUri are doubtful, the only way I could
* think of this happening is trying to get parent of a SAF permission which would
* step above the permission and would only be useful in the case of redundant stacked permissions,
* ie: luckily grabbing a parent tree that does have permission.
*/
// if (DocumentsContract.isDocumentUri(mContext, mUri)) // has tree or document segment
// {
/**
* It's very important we retain tree id because tree is what defines the permission.
* Even if a file is under a permission root if the tree id is corrupted it will
* fail to gain write permission! */
// TODO: Doubtful, but can you game document uris to write where you're not allowed
// using a known valid tree?
if (DocumentUtil.hasTreeDocumentId(mUri)) // has tree segment
{
parentUri = DocumentsContract.buildDocumentUriUsingTree(mUri, parentId);
}
else // has only document segment, if there are write restrictions this is useless!
{
parentUri = DocumentsContract.buildDocumentUri(mUri.getAuthority(), parentId);
}
// }
// else // attempt to build a tree...this is of dubious usefulness as a permission would have to line up
// {
// parentUri = DocumentsContract.buildTreeDocumentUri(mUri.getAuthority(), parentId);
// }
return UsefulDocumentFile.fromUri(mContext, parentUri);
}