下面列出了android.content.ContentResolver#acquireUnstableContentProviderClient ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static boolean uncompressDocument(ContentResolver resolver, Uri fromDocumentUri) {
final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
fromDocumentUri.getAuthority());
try {
final Bundle in = new Bundle();
in.putString(Document.COLUMN_DOCUMENT_ID, getDocumentId(fromDocumentUri));
in.putParcelable(DocumentsContract.EXTRA_URI, fromDocumentUri);
resolver.call(fromDocumentUri, METHOD_UNCOMPRESS_DOCUMENT, null, in);
return true;
} catch (Exception e) {
Log.w(TAG, "Failed to uncompress document", e);
return false;
} finally {
ContentProviderClientCompat.releaseQuietly(client);
}
}
/**
* Return thumbnail representing the document at the given URI. Callers are
* responsible for their own in-memory caching.
*
* @param documentUri document to return thumbnail for, which must have
* {@link Document#FLAG_SUPPORTS_THUMBNAIL} set.
* @param size optimal thumbnail size desired. A provider may return a
* thumbnail of a different size, but never more than double the
* requested size.
* @param signal signal used to indicate if caller is no longer interested
* in the thumbnail.
* @return decoded thumbnail, or {@code null} if problem was encountered.
* @see DocumentsProvider#openDocumentThumbnail(String, Point,
* android.os.CancellationSignal)
*/
public static Bitmap getDocumentThumbnail(
ContentResolver resolver, Uri documentUri, Point size, CancellationSignal signal)
throws FileNotFoundException {
final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
documentUri.getAuthority());
try {
return getDocumentThumbnail(client, documentUri, size, signal);
} catch (Exception e) {
if (!(e instanceof OperationCanceledException)) {
Log.w(TAG, "Failed to load thumbnail for " + documentUri + ": " + e);
}
rethrowIfNecessary(resolver, e);
return null;
} finally {
ContentProviderClient.releaseQuietly(client);
}
}
/**
* Delete the given document.
*
* @param documentUri document with {@link Document#FLAG_SUPPORTS_DELETE}
* @return if the document was deleted successfully.
*/
public static boolean deleteDocument(ContentResolver resolver, Uri documentUri)
throws FileNotFoundException {
final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
documentUri.getAuthority());
try {
deleteDocument(client, documentUri);
return true;
} catch (Exception e) {
Log.w(TAG, "Failed to delete document", e);
rethrowIfNecessary(resolver, e);
return false;
} finally {
ContentProviderClient.releaseQuietly(client);
}
}
public static boolean compressDocument(ContentResolver resolver, Uri fromDocumentUri, ArrayList<String> fromDocumentIds) {
final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
fromDocumentUri.getAuthority());
try {
final Bundle in = new Bundle();
in.putString(Document.COLUMN_DOCUMENT_ID, getDocumentId(fromDocumentUri));
in.putParcelable(DocumentsContract.EXTRA_URI, fromDocumentUri);
in.putStringArrayList(DocumentsContract.EXTRA_DOCUMENTS_COMPRESS, fromDocumentIds);
resolver.call(fromDocumentUri, METHOD_COMPRESS_DOCUMENT, null, in);
return true;
} catch (Exception e) {
Log.w(TAG, "Failed to compress document", e);
return false;
} finally {
ContentProviderClientCompat.releaseQuietly(client);
}
}
/**
* Finds the canonical path from the top of the document tree.
*
* The {@link Path#getPath()} of the return value contains the document ID
* of all documents along the path from the top the document tree to the
* requested document, both inclusive.
*
* The {@link Path#getRootId()} of the return value returns {@code null}.
*
* @param treeUri treeUri of the document which path is requested.
* @return the path of the document, or {@code null} if failed.
* @see DocumentsProvider#findDocumentPath(String, String)
*/
public static Path findDocumentPath(ContentResolver resolver, Uri treeUri)
throws FileNotFoundException {
checkArgument(isTreeUri(treeUri), treeUri + " is not a tree uri.");
final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
treeUri.getAuthority());
try {
return findDocumentPath(client, treeUri);
} catch (Exception e) {
Log.w(TAG, "Failed to find path", e);
rethrowIfNecessary(resolver, e);
return null;
} finally {
ContentProviderClient.releaseQuietly(client);
}
}
public static boolean uncompressDocument(ContentResolver resolver, Uri fromDocumentUri) {
final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
fromDocumentUri.getAuthority());
try {
final Bundle in = new Bundle();
in.putString(Document.COLUMN_DOCUMENT_ID, getDocumentId(fromDocumentUri));
in.putParcelable(DocumentsContract.EXTRA_URI, fromDocumentUri);
resolver.call(fromDocumentUri, METHOD_UNCOMPRESS_DOCUMENT, null, in);
return true;
} catch (Exception e) {
Log.w(TAG, "Failed to uncompress document", e);
return false;
} finally {
ContentProviderClientCompat.releaseQuietly(client);
}
}
/**
* Obtains a list of slices that are descendants of the specified Uri.
* <p>
* Not all slice providers will implement this functionality, in which case,
* an empty collection will be returned.
*
* @param uri The uri to look for descendants under.
* @return All slices within the space.
* @see SliceProvider#onGetSliceDescendants(Uri)
*/
@WorkerThread
public @NonNull Collection<Uri> getSliceDescendants(@NonNull Uri uri) {
ContentResolver resolver = mContext.getContentResolver();
try (ContentProviderClient provider = resolver.acquireUnstableContentProviderClient(uri)) {
Bundle extras = new Bundle();
extras.putParcelable(SliceProvider.EXTRA_BIND_URI, uri);
final Bundle res = provider.call(SliceProvider.METHOD_GET_DESCENDANTS, null, extras);
return res.getParcelableArrayList(SliceProvider.EXTRA_SLICE_DESCENDANTS);
} catch (RemoteException e) {
Log.e(TAG, "Unable to get slice descendants", e);
}
return Collections.emptyList();
}
/**
* Moves the given document under a new parent.
*
* @param sourceDocumentUri document with {@link Document#FLAG_SUPPORTS_MOVE}
* @param sourceParentDocumentUri parent document of the document to move.
* @param targetParentDocumentUri document which will become a new parent of the source
* document.
* @return the moved document, or {@code null} if failed.
*/
public static Uri moveDocument(ContentResolver resolver, Uri sourceDocumentUri,
Uri sourceParentDocumentUri, Uri targetParentDocumentUri) {
final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
sourceDocumentUri.getAuthority());
try {
return moveDocument(client, sourceDocumentUri, sourceParentDocumentUri,
targetParentDocumentUri);
} catch (Exception e) {
Log.w(TAG, "Failed to move document", e);
return null;
} finally {
ContentProviderClientCompat.releaseQuietly(client);
}
}
/**
* Turns a slice intent into a slice uri. Expects an explicit intent.
* <p>
* This goes through a several stage resolution process to determine if any slice
* can represent this intent.
* <ol>
* <li> If the intent contains data that {@link ContentResolver#getType} is
* {@link SliceProvider#SLICE_TYPE} then the data will be returned.</li>
* <li>If the intent explicitly points at an activity, and that activity has
* meta-data for key {@link #SLICE_METADATA_KEY}, then the Uri specified there will be
* returned.</li>
* <li>Lastly, if the intent with {@link #CATEGORY_SLICE} added resolves to a provider, then
* the provider will be asked to {@link SliceProvider#onMapIntentToUri} and that result
* will be returned.</li>
* <li>If no slice is found, then {@code null} is returned.</li>
* </ol>
* @param intent The intent associated with a slice.
* @return The Slice Uri provided by the app or null if none exists.
* @see Slice
* @see SliceProvider#onMapIntentToUri(Intent)
* @see Intent
*/
public @Nullable Uri mapIntentToUri(@NonNull Intent intent) {
ContentResolver resolver = mContext.getContentResolver();
final Uri staticUri = resolveStatic(intent, resolver);
if (staticUri != null) return staticUri;
// Otherwise ask the app
String authority = getAuthority(intent);
if (authority == null) return null;
Uri uri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
.authority(authority).build();
try (ContentProviderClient provider = resolver.acquireUnstableContentProviderClient(uri)) {
if (provider == null) {
Log.w(TAG, String.format("Unknown URI: %s", uri));
return null;
}
Bundle extras = new Bundle();
extras.putParcelable(SliceProvider.EXTRA_INTENT, intent);
final Bundle res = provider.call(SliceProvider.METHOD_MAP_ONLY_INTENT, null, extras);
if (res == null) {
return null;
}
return res.getParcelable(SliceProvider.EXTRA_SLICE);
} catch (RemoteException e) {
// Arbitrary and not worth documenting, as Activity
// Manager will kill this process shortly anyway.
return null;
}
}
/**
* Copies the given document.
*
* @param sourceDocumentUri document with {@link DocumentsContract.Document#FLAG_SUPPORTS_COPY}
* @param targetParentDocumentUri document which will become a parent of the source
* document's copy.
* @return the copied document, or {@code null} if failed.
*/
public static Uri copyDocument(ContentResolver resolver, Uri sourceDocumentUri,
Uri targetParentDocumentUri) {
final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
sourceDocumentUri.getAuthority());
try {
return copyDocument(client, sourceDocumentUri, targetParentDocumentUri);
} catch (Exception e) {
Log.w(TAG, "Failed to copy document", e);
return null;
} finally {
ContentProviderClientCompat.releaseQuietly(client);
}
}
/**
* Removes the given document from a parent directory.
*
* <p>In contrast to {@link #deleteDocument} it requires specifying the parent.
* This method is especially useful if the document can be in multiple parents.
*
* @param documentUri document with {@link Document#FLAG_SUPPORTS_REMOVE}
* @param parentDocumentUri parent document of the document to remove.
* @return true if the document was removed successfully.
*/
public static boolean removeDocument(ContentResolver resolver, Uri documentUri,
Uri parentDocumentUri) {
final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
documentUri.getAuthority());
try {
removeDocument(client, documentUri, parentDocumentUri);
return true;
} catch (Exception e) {
Log.w(TAG, "Failed to remove document", e);
return false;
} finally {
ContentProviderClientCompat.releaseQuietly(client);
}
}
/**
* Removes the given document from a parent directory.
*
* <p>In contrast to {@link #deleteDocument} it requires specifying the parent.
* This method is especially useful if the document can be in multiple parents.
*
* @param documentUri document with {@link Document#FLAG_SUPPORTS_REMOVE}
* @param parentDocumentUri parent document of the document to remove.
* @return true if the document was removed successfully.
*/
public static boolean removeDocument(ContentResolver resolver, Uri documentUri,
Uri parentDocumentUri) {
final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
documentUri.getAuthority());
try {
removeDocument(client, documentUri, parentDocumentUri);
return true;
} catch (Exception e) {
Log.w(TAG, "Failed to remove document", e);
return false;
} finally {
ContentProviderClientCompat.releaseQuietly(client);
}
}
/**
* Moves the given document under a new parent.
*
* @param sourceDocumentUri document with {@link Document#FLAG_SUPPORTS_MOVE}
* @param sourceParentDocumentUri parent document of the document to move.
* @param targetParentDocumentUri document which will become a new parent of the source
* document.
* @return the moved document, or {@code null} if failed.
*/
public static Uri moveDocument(ContentResolver resolver, Uri sourceDocumentUri,
Uri sourceParentDocumentUri, Uri targetParentDocumentUri) {
final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
sourceDocumentUri.getAuthority());
try {
return moveDocument(client, sourceDocumentUri, sourceParentDocumentUri,
targetParentDocumentUri);
} catch (Exception e) {
Log.w(TAG, "Failed to move document", e);
return null;
} finally {
ContentProviderClientCompat.releaseQuietly(client);
}
}
/**
* Copies the given document.
*
* @param sourceDocumentUri document with {@link DocumentsContract.Document#FLAG_SUPPORTS_COPY}
* @param targetParentDocumentUri document which will become a parent of the source
* document's copy.
* @return the copied document, or {@code null} if failed.
*/
public static Uri copyDocument(ContentResolver resolver, Uri sourceDocumentUri,
Uri targetParentDocumentUri) {
final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
sourceDocumentUri.getAuthority());
try {
return copyDocument(client, sourceDocumentUri, targetParentDocumentUri);
} catch (Exception e) {
Log.w(TAG, "Failed to copy document", e);
return null;
} finally {
ContentProviderClientCompat.releaseQuietly(client);
}
}
private static String getFilePath(ContentResolver resolver, Uri mediaUri)
throws RemoteException {
try (ContentProviderClient client =
resolver.acquireUnstableContentProviderClient(AUTHORITY)) {
final Cursor c = client.query(
mediaUri,
new String[]{ MediaColumns.DATA },
null, /* selection */
null, /* selectionArg */
null /* sortOrder */);
final String path;
try {
if (c.getCount() == 0) {
throw new IllegalStateException("Not found media file under URI: " + mediaUri);
}
if (!c.moveToFirst()) {
throw new IllegalStateException("Failed to move cursor to the first item.");
}
path = c.getString(0);
} finally {
IoUtils.closeQuietly(c);
}
return path;
}
}
private static Uri getDocumentUri(
ContentResolver resolver, String path, List<UriPermission> uriPermissions)
throws RemoteException {
try (ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
DocumentsContract.EXTERNAL_STORAGE_PROVIDER_AUTHORITY)) {
final Bundle in = new Bundle();
in.putParcelableList(
DocumentsContract.EXTERNAL_STORAGE_PROVIDER_AUTHORITY + ".extra.uriPermissions",
uriPermissions);
final Bundle out = client.call("getDocumentId", path, in);
return out.getParcelable(DocumentsContract.EXTRA_URI);
}
}
/**
* Create a new document with given MIME type and display name.
*
* @param parentDocumentUri directory with
* {@link DocumentsContract.Document#FLAG_DIR_SUPPORTS_CREATE}
* @param mimeType MIME type of new document
* @param displayName name of new document
* @return newly created document, or {@code null} if failed
*/
public static Uri createDocument(ContentResolver resolver, Uri parentDocumentUri,
String mimeType, String displayName) {
final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
parentDocumentUri.getAuthority());
try {
return createDocument(client, parentDocumentUri, mimeType, displayName);
} catch (Exception e) {
Log.w(TAG, "Failed to create document", e);
return null;
} finally {
ContentProviderClientCompat.releaseQuietly(client);
}
}
/**
* Delete the given document.
*
* @param documentUri document with {@link Document#FLAG_SUPPORTS_DELETE}
* @return if the document was deleted successfully.
*/
public static boolean deleteDocument(ContentResolver resolver, Uri documentUri) {
final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
documentUri.getAuthority());
try {
deleteDocument(client, documentUri);
return true;
} catch (Exception e) {
Log.w(TAG, "Failed to delete document", e);
return false;
} finally {
ContentProviderClientCompat.releaseQuietly(client);
}
}
/**
* Returns metadata associated with the document. The type of metadata returned
* is specific to the document type. For example the data returned for an image
* file will likely consist primarily or soley of EXIF metadata.
*
* <p>The returned {@link Bundle} will contain zero or more entries depending
* on the type of data supported by the document provider.
*
* <ol>
* <li>A {@link DocumentsContract.METADATA_TYPES} containing a {@code String[]} value.
* The string array identifies the type or types of metadata returned. Each
* value in the can be used to access a {@link Bundle} of data
* containing that type of data.
* <li>An entry each for each type of returned metadata. Each set of metadata is
* itself represented as a bundle and accessible via a string key naming
* the type of data.
* </ol>
*
* <p>Example:
* <p><pre><code>
* Bundle metadata = DocumentsContract.getDocumentMetadata(client, imageDocUri, tags);
* if (metadata.containsKey(DocumentsContract.METADATA_EXIF)) {
* Bundle exif = metadata.getBundle(DocumentsContract.METADATA_EXIF);
* int imageLength = exif.getInt(ExifInterface.TAG_IMAGE_LENGTH);
* }
* </code></pre>
*
* @param documentUri a Document URI
* @return a Bundle of Bundles.
* {@hide}
*/
public static Bundle getDocumentMetadata(ContentResolver resolver, Uri documentUri)
throws FileNotFoundException {
final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
documentUri.getAuthority());
try {
return getDocumentMetadata(client, documentUri);
} catch (Exception e) {
Log.w(TAG, "Failed to get document metadata");
rethrowIfNecessary(resolver, e);
return null;
} finally {
ContentProviderClient.releaseQuietly(client);
}
}
/**
* Creates an intent for obtaining a web link for the specified document.
*
* <p>Note, that due to internal limitations, if there is already a web link
* intent created for the specified document but with different options,
* then it may be overridden.
*
* <p>Providers are required to show confirmation UI for all new permissions granted
* for the linked document.
*
* <p>If list of recipients is known, then it should be passed in options as
* {@link Intent#EXTRA_EMAIL} as a list of email addresses. Note, that
* this is just a hint for the provider, which can ignore the list. In either
* case the provider is required to show a UI for letting the user confirm
* any new permission grants.
*
* <p>Note, that the entire <code>options</code> bundle will be sent to the provider
* backing the passed <code>uri</code>. Make sure that you trust the provider
* before passing any sensitive information.
*
* <p>Since this API may show a UI, it cannot be called from background.
*
* <p>In order to obtain the Web Link use code like this:
* <pre><code>
* void onSomethingHappened() {
* IntentSender sender = DocumentsContract.createWebLinkIntent(<i>...</i>);
* if (sender != null) {
* startIntentSenderForResult(
* sender,
* WEB_LINK_REQUEST_CODE,
* null, 0, 0, 0, null);
* }
* }
*
* <i>(...)</i>
*
* void onActivityResult(int requestCode, int resultCode, Intent data) {
* if (requestCode == WEB_LINK_REQUEST_CODE && resultCode == RESULT_OK) {
* Uri weblinkUri = data.getData();
* <i>...</i>
* }
* }
* </code></pre>
*
* @param uri uri for the document to create a link to.
* @param options Extra information for generating the link.
* @return an intent sender to obtain the web link, or null if the document
* is not linkable, or creating the intent sender failed.
* @see DocumentsProvider#createWebLinkIntent(String, Bundle)
* @see Intent#EXTRA_EMAIL
*/
public static IntentSender createWebLinkIntent(ContentResolver resolver, Uri uri,
Bundle options) throws FileNotFoundException {
final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
uri.getAuthority());
try {
return createWebLinkIntent(client, uri, options);
} catch (Exception e) {
Log.w(TAG, "Failed to create a web link intent", e);
rethrowIfNecessary(resolver, e);
return null;
} finally {
ContentProviderClient.releaseQuietly(client);
}
}