public static final Uri getUriToDrawable(@NonNull Context context,
@AnyRes int drawableId) {
Uri imageUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + context.getResources().getResourcePackageName(drawableId)
+ '/' + context.getResources().getResourceTypeName(drawableId)
+ '/' + context.getResources().getResourceEntryName(drawableId) );
return imageUri;
}
基于上述内容 - 适用于任何资源的微调版本:
public static final Uri getUriToResource(@NonNull Context context,
@AnyRes int resId)
throws Resources.NotFoundException {
Resources res = context.getResources();
Uri resUri = getUriToResource(res,resId);
return resUri;
}
public static final Uri getUriToResource(@NonNull Resources res,
@AnyRes int resId)
throws Resources.NotFoundException {
Uri resUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE +
"://" + res.getResourcePackageName(resId)
+ '/' + res.getResourceTypeName(resId)
+ '/' + res.getResourceEntryName(resId));
return resUri;
}
一些信息:
From the Java Language spec.:
"17.5 Final Field Semantics
... when the object is seen by another thread, that thread will always
see the correctly constructed version of that object's final fields.
It will also see versions of any object or array referenced by
those final fields that are at least as up-to-date as the final fields
are."
In that same vein, all non-transient fields within Uri
implementations should be final and immutable so as to ensure true
immutability for clients even when they don't use proper concurrency
control.
For reference, from RFC 2396:
"4.3. Parsing a URI Reference
A URI reference is typically parsed according to the four main
components and fragment identifier in order to determine what
components are present and whether the reference is relative or
absolute. The individual components are then parsed for their
subparts and, if not opaque, to verify their validity.
Although the BNF defines what is allowed in each component, it is
ambiguous in terms of differentiating between an authority component
and a path component that begins with two slash characters. The
greedy algorithm is used for disambiguation: the left-most matching
rule soaks up as much of the URI reference string as it is capable of
matching. In other words, the authority component wins."
...
3. URI Syntactic Components
The URI syntax is dependent upon the scheme.
In general, absolute URI are written as follows:
<scheme>:<scheme-specific-part>
An absolute URI contains the name of the scheme being used (<scheme>)
followed by a colon (":") and then a string (the <scheme-specific-part>)
whose interpretation depends on the scheme.
The URI syntax does not require that the scheme-specific-part have any
general structure or set of semantics which is common among all URI.
However, a subset of URI do share a common syntax for representing
hierarchical relationships within the namespace. This "generic URI"
syntax consists of a sequence of four main components:
<scheme>://<authority><path>?<query>
来源:
争议
这个答案是正确的,但是有关final fields的部分是错误的 - 这与回答无关 - Boris Treukhov
@BorisTreukhov - 请向我们详细说明您所说的“有关final fields的部分是不正确的”是什么意思 - 问题 - 如何获得uri以构造可以解析的方式(uri如何解析?请参见答案)。
package android.net;
public abstract class Uri implements Parcelable, Comparable<Uri> { ... }
Uri.parse()
不应该抛出这个异常。当您解析一个Uri
时,它只检查是否为空引用,而不是实际解析它。 - Michaelstream
和toString()
吗? - cagcak