Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
沉迷打码的小凳子
glide
提交
b51cd256
G
glide
项目概览
沉迷打码的小凳子
/
glide
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
glide
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b51cd256
编写于
9月 25, 2014
作者:
S
Sam Judd
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Handle asset manager Uris.
Fixes #155.
上级
b4ed2bbf
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
197 addition
and
5 deletion
+197
-5
library/src/androidTest/java/com/bumptech/glide/load/model/stream/UriLoaderTest.java
...a/com/bumptech/glide/load/model/stream/UriLoaderTest.java
+15
-0
library/src/main/java/com/bumptech/glide/load/data/AssetPathFetcher.java
...n/java/com/bumptech/glide/load/data/AssetPathFetcher.java
+72
-0
library/src/main/java/com/bumptech/glide/load/data/FileDescriptorAssetPathFetcher.java
...ptech/glide/load/data/FileDescriptorAssetPathFetcher.java
+25
-0
library/src/main/java/com/bumptech/glide/load/data/StreamAssetPathFetcher.java
.../com/bumptech/glide/load/data/StreamAssetPathFetcher.java
+25
-0
library/src/main/java/com/bumptech/glide/load/model/AssetUriParser.java
...in/java/com/bumptech/glide/load/model/AssetUriParser.java
+32
-0
library/src/main/java/com/bumptech/glide/load/model/UriLoader.java
...rc/main/java/com/bumptech/glide/load/model/UriLoader.java
+12
-1
library/src/main/java/com/bumptech/glide/load/model/file_descriptor/FileDescriptorUriLoader.java
...e/load/model/file_descriptor/FileDescriptorUriLoader.java
+8
-2
library/src/main/java/com/bumptech/glide/load/model/stream/StreamUriLoader.java
...com/bumptech/glide/load/model/stream/StreamUriLoader.java
+8
-2
未找到文件。
library/src/androidTest/java/com/bumptech/glide/load/model/stream/UriLoaderTest.java
浏览文件 @
b51cd256
...
...
@@ -33,17 +33,25 @@ public class UriLoaderTest {
private
UriLoader
loader
;
private
DataFetcher
<
InputStream
>
localUriFetcher
;
private
ModelLoader
<
GlideUrl
,
InputStream
>
urlLoader
;
private
DataFetcher
<
InputStream
>
assetUriFetcher
;
@SuppressWarnings
(
"uncecked"
)
@Before
public
void
setUp
()
throws
Exception
{
urlLoader
=
mock
(
ModelLoader
.
class
);
localUriFetcher
=
mock
(
DataFetcher
.
class
);
assetUriFetcher
=
mock
(
DataFetcher
.
class
);
loader
=
new
UriLoader
<
InputStream
>(
Robolectric
.
application
,
urlLoader
)
{
@Override
protected
DataFetcher
<
InputStream
>
getLocalUriFetcher
(
Context
context
,
Uri
uri
)
{
return
localUriFetcher
;
}
@Override
protected
DataFetcher
<
InputStream
>
getAssetPathFetcher
(
Context
context
,
String
path
)
{
return
assetUriFetcher
;
}
};
}
...
...
@@ -68,6 +76,13 @@ public class UriLoaderTest {
assertEquals
(
localUriFetcher
,
dataFetcher
);
}
@Test
public
void
testHandlesAssetUris
()
{
Uri
assetUri
=
Uri
.
parse
(
"file:///android_asset/assetName"
);
DataFetcher
fetcher
=
loader
.
getResourceFetcher
(
assetUri
,
IMAGE_SIDE
,
IMAGE_SIDE
);
assertEquals
(
assetUriFetcher
,
fetcher
);
}
@Test
public
void
testHandlesHttpUris
()
throws
MalformedURLException
{
Uri
httpUri
=
Uri
.
parse
(
"http://www.google.com"
);
...
...
library/src/main/java/com/bumptech/glide/load/data/AssetPathFetcher.java
0 → 100644
浏览文件 @
b51cd256
package
com.bumptech.glide.load.data
;
import
android.content.res.AssetManager
;
import
android.util.Log
;
import
com.bumptech.glide.Priority
;
import
java.io.IOException
;
/**
* An abstract class for obtaining data for an asset path using an {@link android.content.res.AssetManager}.
*
* @param <T> The type of data obtained from the asset path (InputStream, FileDescriptor etc).
*/
public
abstract
class
AssetPathFetcher
<
T
>
implements
DataFetcher
<
T
>
{
private
static
final
String
TAG
=
"AssetUriFetcher"
;
private
final
String
assetPath
;
private
final
AssetManager
assetManager
;
private
T
data
;
public
AssetPathFetcher
(
AssetManager
assetManager
,
String
assetPath
)
{
this
.
assetManager
=
assetManager
;
this
.
assetPath
=
assetPath
;
}
@Override
public
T
loadData
(
Priority
priority
)
throws
Exception
{
data
=
loadResource
(
assetManager
,
assetPath
);
return
data
;
}
@Override
public
void
cleanup
()
{
if
(
data
==
null
)
{
return
;
}
try
{
close
(
data
);
}
catch
(
IOException
e
)
{
if
(
Log
.
isLoggable
(
TAG
,
Log
.
VERBOSE
))
{
Log
.
v
(
TAG
,
"Failed to close data"
,
e
);
}
}
}
@Override
public
String
getId
()
{
return
assetPath
;
}
@Override
public
void
cancel
()
{
// Do nothing.
}
/**
* Opens the given asset path with the given {@link android.content.res.AssetManager} and returns the conrete data
* type returned by the AssetManager.
*
* @param assetManager An AssetManager to use to open the given path.
* @param path A string path pointing to a resource in assets to open.
*/
protected
abstract
T
loadResource
(
AssetManager
assetManager
,
String
path
)
throws
IOException
;
/**
* Closes the concrete data type if necessary.
*
* @param data The data to close.
* @throws IOException
*/
protected
abstract
void
close
(
T
data
)
throws
IOException
;
}
library/src/main/java/com/bumptech/glide/load/data/FileDescriptorAssetPathFetcher.java
0 → 100644
浏览文件 @
b51cd256
package
com.bumptech.glide.load.data
;
import
android.content.res.AssetManager
;
import
android.os.ParcelFileDescriptor
;
import
java.io.IOException
;
/**
* Fetches an {@link android.os.ParcelFileDescriptor} for an asset path.
*/
public
class
FileDescriptorAssetPathFetcher
extends
AssetPathFetcher
<
ParcelFileDescriptor
>
{
public
FileDescriptorAssetPathFetcher
(
AssetManager
assetManager
,
String
assetPath
)
{
super
(
assetManager
,
assetPath
);
}
@Override
protected
ParcelFileDescriptor
loadResource
(
AssetManager
assetManager
,
String
path
)
throws
IOException
{
return
assetManager
.
openFd
(
path
).
getParcelFileDescriptor
();
}
@Override
protected
void
close
(
ParcelFileDescriptor
data
)
throws
IOException
{
data
.
close
();
}
}
library/src/main/java/com/bumptech/glide/load/data/StreamAssetPathFetcher.java
0 → 100644
浏览文件 @
b51cd256
package
com.bumptech.glide.load.data
;
import
android.content.res.AssetManager
;
import
java.io.IOException
;
import
java.io.InputStream
;
/**
* Fetches an {@link java.io.InputStream} for an asset path.
*/
public
class
StreamAssetPathFetcher
extends
AssetPathFetcher
<
InputStream
>
{
public
StreamAssetPathFetcher
(
AssetManager
assetManager
,
String
assetPath
)
{
super
(
assetManager
,
assetPath
);
}
@Override
protected
InputStream
loadResource
(
AssetManager
assetManager
,
String
path
)
throws
IOException
{
return
assetManager
.
open
(
path
);
}
@Override
protected
void
close
(
InputStream
data
)
throws
IOException
{
data
.
close
();
}
}
library/src/main/java/com/bumptech/glide/load/model/AssetUriParser.java
0 → 100644
浏览文件 @
b51cd256
package
com.bumptech.glide.load.model
;
import
android.content.ContentResolver
;
import
android.net.Uri
;
/**
* A utility class for parsing Asset uris that look like: file:///android_asset/some/path/in/assets/folder.
*/
class
AssetUriParser
{
private
static
final
String
ASSET_PATH_SEGMENT
=
"android_asset"
;
private
static
final
String
ASSET_PREFIX
=
ContentResolver
.
SCHEME_FILE
+
":///"
+
ASSET_PATH_SEGMENT
+
"/"
;
private
static
final
int
ASSET_PREFIX_LENGTH
=
ASSET_PREFIX
.
length
();
/**
* Returns true if the given {@link android.net.Uri} matches the asset uri pattern.
*/
public
static
boolean
isAssetUri
(
Uri
uri
)
{
return
ContentResolver
.
SCHEME_FILE
.
equals
(
uri
.
getScheme
())
&&
!
uri
.
getPathSegments
().
isEmpty
()
&&
ASSET_PATH_SEGMENT
.
equals
(
uri
.
getPathSegments
().
get
(
0
));
}
/**
* Returns the string path for the given asset uri.
*
* <p>
* Assumes the given {@link android.net.Uri} is in fact an asset uri.
* </p>
*/
public
static
String
toAssetPath
(
Uri
uri
)
{
return
uri
.
toString
().
substring
(
ASSET_PREFIX_LENGTH
);
}
}
library/src/main/java/com/bumptech/glide/load/model/UriLoader.java
浏览文件 @
b51cd256
...
...
@@ -13,6 +13,10 @@ import com.bumptech.glide.load.data.DataFetcher;
* @param <T> The type of data that will be retrieved for {@link android.net.Uri}s.
*/
public
abstract
class
UriLoader
<
T
>
implements
ModelLoader
<
Uri
,
T
>
{
private
static
final
String
ASSET_PATH_SEGMENT
=
"android_asset"
;
private
static
final
String
ASSET_PREFIX
=
ContentResolver
.
SCHEME_FILE
+
":///"
+
ASSET_PATH_SEGMENT
+
"/"
;
private
static
final
int
ASSET_PREFIX_LENGTH
=
ASSET_PREFIX
.
length
();
private
final
Context
context
;
private
final
ModelLoader
<
GlideUrl
,
T
>
urlLoader
;
...
...
@@ -27,7 +31,12 @@ public abstract class UriLoader<T> implements ModelLoader<Uri, T> {
DataFetcher
<
T
>
result
=
null
;
if
(
isLocalUri
(
scheme
))
{
result
=
getLocalUriFetcher
(
context
,
model
);
if
(
AssetUriParser
.
isAssetUri
(
model
))
{
String
path
=
AssetUriParser
.
toAssetPath
(
model
);
result
=
getAssetPathFetcher
(
context
,
path
);
}
else
{
result
=
getLocalUriFetcher
(
context
,
model
);
}
}
else
if
(
urlLoader
!=
null
&&
(
"http"
.
equals
(
scheme
)
||
"https"
.
equals
(
scheme
)))
{
result
=
urlLoader
.
getResourceFetcher
(
new
GlideUrl
(
model
.
toString
()),
width
,
height
);
}
...
...
@@ -37,6 +46,8 @@ public abstract class UriLoader<T> implements ModelLoader<Uri, T> {
protected
abstract
DataFetcher
<
T
>
getLocalUriFetcher
(
Context
context
,
Uri
uri
);
protected
abstract
DataFetcher
<
T
>
getAssetPathFetcher
(
Context
context
,
String
path
);
private
static
boolean
isLocalUri
(
String
scheme
)
{
return
ContentResolver
.
SCHEME_FILE
.
equals
(
scheme
)
||
ContentResolver
.
SCHEME_CONTENT
.
equals
(
scheme
)
...
...
library/src/main/java/com/bumptech/glide/load/model/file_descriptor/FileDescriptorUriLoader.java
浏览文件 @
b51cd256
...
...
@@ -4,13 +4,14 @@ import android.content.Context;
import
android.net.Uri
;
import
android.os.ParcelFileDescriptor
;
import
com.bumptech.glide.Glide
;
import
com.bumptech.glide.load.data.DataFetcher
;
import
com.bumptech.glide.load.data.FileDescriptorAssetPathFetcher
;
import
com.bumptech.glide.load.data.FileDescriptorLocalUriFetcher
;
import
com.bumptech.glide.load.model.GenericLoaderFactory
;
import
com.bumptech.glide.load.model.GlideUrl
;
import
com.bumptech.glide.load.model.ModelLoader
;
import
com.bumptech.glide.load.model.ModelLoaderFactory
;
import
com.bumptech.glide.load.model.UriLoader
;
import
com.bumptech.glide.load.data.DataFetcher
;
import
com.bumptech.glide.load.data.FileDescriptorLocalUriFetcher
;
/**
* A {@link ModelLoader} For translating {@link Uri} models for local uris into {@link ParcelFileDescriptor} data.
...
...
@@ -45,4 +46,9 @@ public class FileDescriptorUriLoader extends UriLoader<ParcelFileDescriptor> imp
protected
DataFetcher
<
ParcelFileDescriptor
>
getLocalUriFetcher
(
Context
context
,
Uri
uri
)
{
return
new
FileDescriptorLocalUriFetcher
(
context
,
uri
);
}
@Override
protected
DataFetcher
<
ParcelFileDescriptor
>
getAssetPathFetcher
(
Context
context
,
String
assetPath
)
{
return
new
FileDescriptorAssetPathFetcher
(
context
.
getApplicationContext
().
getAssets
(),
assetPath
);
}
}
library/src/main/java/com/bumptech/glide/load/model/stream/StreamUriLoader.java
浏览文件 @
b51cd256
...
...
@@ -3,13 +3,14 @@ package com.bumptech.glide.load.model.stream;
import
android.content.Context
;
import
android.net.Uri
;
import
com.bumptech.glide.Glide
;
import
com.bumptech.glide.load.data.DataFetcher
;
import
com.bumptech.glide.load.data.StreamAssetPathFetcher
;
import
com.bumptech.glide.load.data.StreamLocalUriFetcher
;
import
com.bumptech.glide.load.model.GenericLoaderFactory
;
import
com.bumptech.glide.load.model.GlideUrl
;
import
com.bumptech.glide.load.model.ModelLoader
;
import
com.bumptech.glide.load.model.ModelLoaderFactory
;
import
com.bumptech.glide.load.model.UriLoader
;
import
com.bumptech.glide.load.data.DataFetcher
;
import
com.bumptech.glide.load.data.StreamLocalUriFetcher
;
import
java.io.InputStream
;
...
...
@@ -48,4 +49,9 @@ public class StreamUriLoader extends UriLoader<InputStream> implements StreamMod
protected
DataFetcher
<
InputStream
>
getLocalUriFetcher
(
Context
context
,
Uri
uri
)
{
return
new
StreamLocalUriFetcher
(
context
,
uri
);
}
@Override
protected
DataFetcher
<
InputStream
>
getAssetPathFetcher
(
Context
context
,
String
assetPath
)
{
return
new
StreamAssetPathFetcher
(
context
.
getApplicationContext
().
getAssets
(),
assetPath
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录