NIMSDK-AOS  9.19.0
NosThumbImageUtil.java
浏览该文件的文档.
1 package com.netease.nimlib.sdk.nos.util;
2 
3 import android.util.DisplayMetrics;
4 
5 import com.netease.nimlib.SDKCache;
6 import com.netease.nimlib.net.http.util.NosUtil;
8 
9 /**
10  * 网易云信云存储图片缩略图URL生成工具
11  */
12 public class NosThumbImageUtil {
13 
14  public static final String makeImageThumbUrl(String url, NosThumbParam.ThumbType thumb, int width, int height) {
15  return NosUtil.appendQueryParams(url, toImageThumbParams(thumb, width, height));
16  }
17 
18  public static final String makeImageThumbUrl(String url, int originW, int originH) {
20  if (originH > 0 && originW > 0) {
21  int ration = (originW > originH ? originW / originH : originH / originW);
23  }
24 
25  int width = SDKCache.getOptions().thumbnailSize;
26  if (width <= 0) {
27  DisplayMetrics dm = SDKCache.getContext().getApplicationContext().getResources().getDisplayMetrics();
28  width = Math.min(dm.widthPixels, dm.heightPixels) / 2;
29  }
30  return NosUtil.appendQueryParams(url, toImageThumbParams(thumb, width, width));
31  }
32 
33  private static final String toImageThumbParams(NosThumbParam.ThumbType thumb, int width, int height) {
34  if (!checkImageThumb(thumb, width, height)) {
35  throw new IllegalArgumentException("width=" + width + ", height=" + height);
36  }
37 
38  StringBuilder sb = new StringBuilder();
39 
40  sb.append("thumbnail=");
41  sb.append(width);
42  sb.append(toImageThumbMethod(thumb));
43  sb.append(height);
44 
45  sb.append("&imageView");
46 
47  return sb.toString();
48  }
49 
50  private static final boolean checkImageThumb(NosThumbParam.ThumbType thumb, int width, int height) {
51  // not allow negative
52  if (width < 0 || height < 0) {
53  return false;
54  }
55 
56  switch (thumb) {
57  case Internal:
58  // not allow both zero
59  return width > 0 || height > 0;
60  case Crop:
61  case External:
62  // not allow either zero
63  return width > 0 && height > 0;
64  }
65 
66  return false;
67  }
68 
69  private static final String toImageThumbMethod(NosThumbParam.ThumbType thumb) {
70  switch (thumb) {
71  case Internal:
72  return "x";
73  case Crop:
74  return "y";
75  case External:
76  return "z";
77  }
78 
79  throw new IllegalArgumentException("thumb: " + thumb);
80  }
81 }
static final String makeImageThumbUrl(String url, NosThumbParam.ThumbType thumb, int width, int height)
static final String makeImageThumbUrl(String url, int originW, int originH)
网易云信云存储图片缩略图URL生成工具
向NOS请求下载缩略图的参数