我用我的Android应用程序从网上下载图片,并显示一个占位符的动画,而图像下载(RemoteImageView,点火包的一部分的API 在这里.
在应用程序的某些点,我需要显示本地资源drawables中的图像,而不是从Web下载.
是否可以使用URL访问本地资源drawable?
nef*_*tou 13
做到这一点的方法:
使用这一行 "android.resource://[your package]/"+[res id]
这是一个例子
Uri myURI = Uri.parse("android.resource://com.example.project/" + R.drawable.myimage);
Run Code Online (Sandbox Code Playgroud)
为方便起见,您可以将其用作方法:
public static Uri getUrl(int res){
return Uri.parse("android.resource://com.example.project/" + res);
}
Run Code Online (Sandbox Code Playgroud)
警告不要context.getPackageName()用作包名的替代,因为它可能返回null值.
本地图像资源没有网址,它们有URI.因此,如果您在drawable中有图像,则可以将它们从资源ID解析为URI.
Uri uri=Uri.parse("R.drawable.image");
Run Code Online (Sandbox Code Playgroud)
但是,如果您还可以将图像放在包的资产文件夹中,并使用其URL访问它们.图像文件的URL将是"file:///android_asset/image.png"您可以使用任一选项.
| 归档时间: |
|
| 查看次数: |
24267 次 |
| 最近记录: |