本文整理汇总了Java中android.provider.DocumentsContract.buildChildDocumentsUriUsingTree方法的典型用法代码示例。如果您正苦于以下问题:Java DocumentsContract.buildChildDocumentsUriUsingTree方法的具体用法?Java DocumentsContract.buildChildDocumentsUriUsingTree怎么用?Java DocumentsContract.buildChildDocumentsUriUsingTree使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.provider.DocumentsContract
的用法示例。
在下文中一共展示了DocumentsContract.buildChildDocumentsUriUsingTree方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: listFiles
import android.provider.DocumentsContract; //导入方法依赖的package包/类
public static Uri[] listFiles(Context context, Uri self) {
ContentResolver resolver = context.getContentResolver();
Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(self, DocumentsContract.getDocumentId(self));
ArrayList<Uri> results = new ArrayList();
Cursor c = null;
try {
c = resolver.query(childrenUri, new String[]{"document_id"}, null, null, null);
while (c.moveToNext()) {
results.add(DocumentsContract.buildDocumentUriUsingTree(self, c.getString(0)));
}
} catch (Exception e) {
Log.w(TAG, "Failed query: " + e);
} finally {
closeQuietly(c);
}
return (Uri[]) results.toArray(new Uri[results.size()]);
}
示例3: getDirItemsNumber
import android.provider.DocumentsContract; //导入方法依赖的package包/类
private static int getDirItemsNumber(Context ctx, Uri u)
{
int itemsNumber = 0;
Cursor c = null;
try
{
ContentResolver cr = ctx.getContentResolver();
String dirId = DocumentsContract.getDocumentId(u);
Uri dirUri = DocumentsContract.buildChildDocumentsUriUsingTree(u, dirId);
c = cr.query(dirUri, projection, null, null, null);
if (c != null)
{
itemsNumber = c.getCount();
}
}
catch (Exception e)
{
// notning to do;
}
finally
{
if (c != null)
{
c.close();
}
}
return itemsNumber;
}
示例4: 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()]);
}
示例5: 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;
}
示例6: getDocumentFileForUri
import android.provider.DocumentsContract; //导入方法依赖的package包/类
public static DocumentFile getDocumentFileForUri(Context context, Uri uri) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException {
Class<?> c = Class.forName("android.support.v4.provider.TreeDocumentFile");
Constructor<?> constructor = c.getDeclaredConstructor(DocumentFile.class, Context.class, Uri.class);
constructor.setAccessible(true);
Uri docUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
docUri = DocumentsContract.buildChildDocumentsUriUsingTree(uri, uri.getLastPathSegment());
else
docUri = buildDocumentUriUsingTree(uri);
return (DocumentFile) constructor.newInstance(null, context, docUri);
}
示例7: getImagesFromTreeUri
import android.provider.DocumentsContract; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private List<Uri> getImagesFromTreeUri(final Uri treeUri, final int maxImages) {
List<Uri> images = new ArrayList<>();
Queue<String> directories = new LinkedList<>();
directories.add(DocumentsContract.getTreeDocumentId(treeUri));
while (images.size() < maxImages && !directories.isEmpty()) {
String parentDocumentId = directories.poll();
final Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(treeUri,
parentDocumentId);
try (Cursor children = getContentResolver().query(childrenUri,
new String[]{DocumentsContract.Document.COLUMN_DOCUMENT_ID, DocumentsContract.Document.COLUMN_MIME_TYPE},
null, null, null)) {
if (children == null) {
continue;
}
while (children.moveToNext()) {
String documentId = children.getString(
children.getColumnIndex(DocumentsContract.Document.COLUMN_DOCUMENT_ID));
String mimeType = children.getString(
children.getColumnIndex(DocumentsContract.Document.COLUMN_MIME_TYPE));
if (DocumentsContract.Document.MIME_TYPE_DIR.equals(mimeType)) {
directories.add(documentId);
} else if (mimeType != null && mimeType.startsWith("image/")) {
// Add images to the list
images.add(DocumentsContract.buildDocumentUriUsingTree(treeUri, documentId));
}
if (images.size() == maxImages) {
break;
}
}
} catch (SecurityException|NullPointerException e) {
// No longer can read this URI, which means no children from this URI
}
}
return images;
}
示例8: addAllImagesFromTree
import android.provider.DocumentsContract; //导入方法依赖的package包/类
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private int addAllImagesFromTree(final List<Uri> allImages, final Uri treeUri, final String parentDocumentId) {
final Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(treeUri,
parentDocumentId);
Cursor children = null;
try {
children = getContentResolver().query(childrenUri,
new String[]{DocumentsContract.Document.COLUMN_DOCUMENT_ID, DocumentsContract.Document.COLUMN_MIME_TYPE},
null, null, null);
} catch (NullPointerException|IllegalArgumentException|SecurityException e) {
Log.e(TAG, "Error reading " + childrenUri, e);
}
if (children == null) {
return 0;
}
int numImagesAdded = 0;
while (children.moveToNext()) {
String documentId = children.getString(
children.getColumnIndex(DocumentsContract.Document.COLUMN_DOCUMENT_ID));
String mimeType = children.getString(
children.getColumnIndex(DocumentsContract.Document.COLUMN_MIME_TYPE));
if (DocumentsContract.Document.MIME_TYPE_DIR.equals(mimeType)) {
// Recursively explore all directories
numImagesAdded += addAllImagesFromTree(allImages, treeUri, documentId);
} else if (mimeType != null && mimeType.startsWith("image/")) {
// Add images to the list
if (allImages != null) {
allImages.add(DocumentsContract.buildDocumentUriUsingTree(treeUri, documentId));
}
numImagesAdded++;
}
}
children.close();
return numImagesAdded;
}
示例9: 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;
}
示例10: handleOpenDocumentTree
import android.provider.DocumentsContract; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void handleOpenDocumentTree(Uri treeUri) {
String parentDocumentId = DocumentsContract.getTreeDocumentId(treeUri);
Uri parentDocumentUri = DocumentsContract.buildDocumentUri(BuildConfig.DOCUMENTS_AUTHORITY, parentDocumentId);
String parentDisplayName = parentDocumentId;
Cursor cursor = getContentResolver().query(parentDocumentUri, null, null, null, null);
try {
// moveToFirst() returns false if the cursor has 0 rows. Very handy for
// "if there's anything to look at, look at it" conditionals.
if (cursor != null && cursor.moveToFirst()) {
// Note it's called "Display Name". This is
// provider-specific, and might not necessarily be the file name.
parentDisplayName = cursor.getString(
cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
mLastReturnedDocumentUri = null;
mLastReturnedDocumentTreeUri = treeUri;
}
} finally {
if (cursor != null)
cursor.close();
}
mReturnedDetailsSwitcher.setDisplayedChild(1);
Uri documentUri = DocumentsContract.buildChildDocumentsUriUsingTree(treeUri, parentDocumentId);
cursor = getContentResolver().query(documentUri, null, null, null, null);
if (cursor == null) {
mReturnedName.setText("");
mReturnedChildren.setText("");
return;
}
mReturnedName.setText(getString(R.string.parent_document_title, cursor.getCount(), parentDisplayName));
StringBuilder children = new StringBuilder();
while (cursor.moveToNext()) {
// Note it's called "Display Name". This is
// provider-specific, and might not necessarily be the file name.
String displayName = cursor.getString(
cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
children.append(displayName);
if (!cursor.isLast()) {
children.append('\n');
}
}
mReturnedChildren.setText(children);
cursor.close();
}