我在我的应用程序中添加了几个示例项,因此当用户第一次看到它时它看起来不那么空。带有示例项的列表应该有一个图像,我要使用的图像已经存储在应用程序的/ res / drawable-folder中。
因为我已经有一个从URI加载项目图像的方法,我想将URI提供给/res/drawable/myImage.jpg,但我似乎无法正确使用它。
流程如下: 使用表示图像URI的字符串创建项目。 将项目列表发送到列表 该列表通过将字符串转换为URL然后运行url.openStream();
将图像加载到后台任务中我为URI尝试了一些选项而没有任何成功。 “android.resource:// .....”说unknow protocoll 找不到“file://”文件
所以现在我对如何解决这个问题感到有点迷失..
答案 0 :(得分:83)
您应该使用ContentResolver
打开资源URI:
Uri uri = Uri.parse("android.resource://your.package.here/drawable/image_name");
InputStream stream = getContentResolver().openInputStream(uri);
您也可以使用此方法打开文件和内容URI。
答案 1 :(得分:40)
/** * get uri to drawable or any other resource type if u wish * @param context - context * @param drawableId - drawable res id * @return - uri */ 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; }
基于以上 - 任何资源的调整版本:
/**
* get uri to any resource type
* @param context - context
* @param resId - resource id
* @throws Resources.NotFoundException if the given ID does not exist.
* @return - Uri to resource by given id
*/
public static final Uri getUriToResource(@NonNull Context context,
@AnyRes int resId)
throws Resources.NotFoundException {
/** Return a Resources instance for your application's package. */
Resources res = context.getResources();
/**
* Creates a Uri which parses the given encoded URI string.
* @param uriString an RFC 2396-compliant, encoded URI
* @throws NullPointerException if uriString is null
* @return Uri for this given uri string
*/
Uri resUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE +
"://" + res.getResourcePackageName(resId)
+ '/' + res.getResourceTypeName(resId)
+ '/' + res.getResourceEntryName(resId));
/** return uri */
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>
来源:
<强>争议强>
这个答案是正确的,但关于最终字段的部分不是 - 它与答案无关 - Boris Treukhov
@BorisTreukhov - 请详细告诉我们你的意思&#34;关于最终字段的部分是不正确的&#34; - 问题 - 如何获得uri ...?构造它可以被解析的方式(如何解析uri?看到答案)
package android.net;
/**
* Immutable URI reference. A URI reference includes a URI and a fragment, the
* component of the URI following a '#'. Builds and parses URI references
* which conform to
* <a href="http://www.faqs.org/rfcs/rfc2396.html">RFC 2396</a>.
*
* <p>In the interest of performance, this class performs little to no
* validation. Behavior is undefined for invalid input. This class is very
* forgiving--in the face of invalid input, it will return garbage
* rather than throw an exception unless otherwise specified.
*/
public abstract class Uri implements Parcelable, Comparable<Uri> { ... }
答案 2 :(得分:27)
这是你真正需要的:
Uri imageUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE +
"://" + getResources().getResourcePackageName(R.drawable.ic_launcher)
+ '/' + getResources().getResourceTypeName(R.drawable.ic_launcher) + '/' + getResources().getResourceEntryName(R.drawable.ic_launcher) );
答案 3 :(得分:3)
您可以使用Uri.Builder
代替字符串串联
Uri imageUri = (new Uri.Builder())
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
.authority(resources.getResourcePackageName(resourceId))
.appendPath(resources.getResourceTypeName(resourceId))
.appendPath(resources.getResourceEntryName(resourceId))
.build()
答案 4 :(得分:0)
以最简单的形式...
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.book);
InputStream iStream = getContentResolver().openInputStream(uri);
其中“ book”是可绘制文件夹中文件的名称。
答案 5 :(得分:-1)
最简单的答案是: Uri.parse(字符串在这里); //因此,要使drawable适合该支架,您只需要这样做。
Uri.parse(getResource().getDrawable(R.drawable.ic_launcher_background).toString());
就是这样。