Android如何根据资源路径Url获取Scheme类型

1,使用方法:

		switch (Scheme.ofUri(imageUri)) {
			case HTTP:
			case HTTPS:
			case FILE:
			case CONTENT:
			case ASSETS:
			case DRAWABLE:
			case UNKNOWN:
			default:
		}


2,工具源码:

	enum Scheme {
		HTTP("http"), HTTPS("https"), FILE("file"), CONTENT("content"), ASSETS("assets"), DRAWABLE("drawable"), UNKNOWN("");

		private String scheme;
		private String uriPrefix;

		Scheme(String scheme) {
			this.scheme = scheme;
			uriPrefix = scheme + "://";
		}

		
		public static Scheme ofUri(String uri) {
			if (uri != null) {
				for (Scheme s : values()) {
					if (s.belongsTo(uri)) {
						return s;
					}
				}
			}
			return UNKNOWN;
		}

		private boolean belongsTo(String uri) {
			return uri.toLowerCase(Locale.US).startsWith(uriPrefix);
		}

		public String wrap(String path) {
			return uriPrefix + path;
		}

		public String crop(String uri) {
			if (!belongsTo(uri)) {
				throw new IllegalArgumentException(String.format("URI [%1$s] doesn't have expected scheme [%2$s]", uri, scheme));
			}
			return uri.substring(uriPrefix.length());
		}
	}


你可能感兴趣的:(编程这些年)