2011-07-06 44 views
37

我在應用程序中添加了幾個示例項目,以便用戶在第一次查看時看起來不會太空。帶有示例項目的列表應該有一個圖像,我要使用的圖像已經存儲在應用程序的/ res/drawable-folder中。獲取存儲在可繪製圖像中的圖像的URI

由於我已經有一個方法,從一個URI加載項目圖像我想要的URI到/res/drawable/myImage.jpg,但我似乎無法得到它的權利。

流程如下: 使用表示圖像URI的字符串創建項目。 將項目列表發送到列表 該列表通過將字符串轉換爲URL,然後運行url.openStream()來加載後臺任務中的圖像。

我已經嘗試了一些沒有任何成功的URI選項。 「android.resource:// ......」說不明protocoll 「文件://」找不到文件

所以現在我有點失去了關於如何解決這個問題..

+1

@Pixie的答案確實有效。 –

回答

69

您應該使用ContentResolver打開資源的URI:

Uri uri = Uri.parse("android.resource://your.package.here/drawable/image_name"); 
InputStream stream = getContentResolver().openInputStream(uri); 

您也可以打開使用此方法文件和內容的URI。

+0

我得到一個MalformedUrlException以下: Uri path = Uri.parse(「android.resource://se.javalia。我的飲料/ drawable/image0109「); 圖像存儲在可繪製的文件夾中,並且是一個jpg文件 – Roland

+0

這很奇怪,因爲Uri.parse()不能拋出這個異常,當你解析一個'Uri'只是檢查'null'引用,但實際上並沒有解析它 – Michael

+0

@Roland你在某處做錯了,這確實有效 –

25

這是你真正需要的:

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)); 
+0

這是否需要一些權限?我不能使用這個 – lirui

+1

我不這麼認爲。什麼是錯誤? – xnagyg

33
/** 
* 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> 

來源:

爭議

這個答案是正確的,但是關於最終場的部分是不是 - 它與回答無關的R - 鮑里斯Treukhov

@BorisTreukhov - 請詳細給我們什麼ü由是指「關於final字段的部分是不正確的」 - 問題 - 如何讓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> { ... } 
+0

真棒它的作品。謝謝 –

+0

這個答案是正確的,但是關於最終字段的部分不是 - 它與回答無關 –

+0

@BorisTreukhov - 請評論編輯 – ceph3us