ImageUtil.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package com.easemob.easeui.oss;
  2. import android.content.Context;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.util.Log;
  6. import com.alibaba.sdk.android.oss.ClientConfiguration;
  7. import com.alibaba.sdk.android.oss.ClientException;
  8. import com.alibaba.sdk.android.oss.OSS;
  9. import com.alibaba.sdk.android.oss.OSSClient;
  10. import com.alibaba.sdk.android.oss.ServiceException;
  11. import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
  12. import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
  13. import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider;
  14. import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
  15. import com.alibaba.sdk.android.oss.model.GetObjectRequest;
  16. import com.alibaba.sdk.android.oss.model.GetObjectResult;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.FileNotFoundException;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.OutputStream;
  24. import java.net.HttpURLConnection;
  25. import java.net.URL;
  26. public class ImageUtil {
  27. static String endpoint = "http://img.lesgoapp.cc";
  28. private static String name = "lesgo";
  29. static Bitmap bitmap = null;
  30. static InputStream inputStream = null;
  31. static OutputStream os = null;
  32. public static Bitmap loadBitmapFromWeb(Context context,String url, final File file) {
  33. try {
  34. OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider("LTAIvkmoS1lN2mCd", "3KLqFPW20hpkXUmFvBwY70P5sxiDcG");
  35. ClientConfiguration conf = new ClientConfiguration();
  36. conf.setConnectionTimeout(15 * 1000); // 连接超时,默认15秒
  37. conf.setSocketTimeout(15 * 1000); // socket超时,默认15秒
  38. conf.setMaxConcurrentRequest(5); // 最大并发请求书,默认5个
  39. conf.setMaxErrorRetry(2); // 失败后最大重试次数,默认2次
  40. OSS oss = new OSSClient(context, endpoint, credentialProvider, conf);
  41. GetObjectRequest get = new GetObjectRequest(name, url);
  42. OSSAsyncTask task = oss.asyncGetObject(get, new OSSCompletedCallback<GetObjectRequest, GetObjectResult>() {
  43. @Override
  44. public void onSuccess(GetObjectRequest request, GetObjectResult result) {
  45. // 请求成功
  46. inputStream = result.getObjectContent();
  47. try {
  48. os = new FileOutputStream(file);
  49. copyStream(inputStream, os);//
  50. bitmap = decodeFile(file);
  51. } catch (FileNotFoundException e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. @Override
  56. public void onFailure(GetObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
  57. // 请求异常
  58. if (clientExcepion != null) {
  59. // 本地异常如网络异常等
  60. clientExcepion.printStackTrace();
  61. }
  62. if (serviceException != null) {
  63. // 服务异常
  64. Log.e("ErrorCode", serviceException.getErrorCode());
  65. Log.e("RequestId", serviceException.getRequestId());
  66. Log.e("HostId", serviceException.getHostId());
  67. Log.e("RawMessage", serviceException.getRawMessage());
  68. }
  69. }
  70. });
  71. return bitmap;
  72. } catch (Exception ex) {
  73. ex.printStackTrace();
  74. return null;
  75. } finally {
  76. try {
  77. if(os != null) os.close();
  78. if(inputStream != null) inputStream.close();
  79. } catch (IOException e) { }
  80. }
  81. }
  82. public static Bitmap decodeFile(File f) {
  83. try {
  84. return BitmapFactory.decodeStream(new FileInputStream(f), null, null);
  85. } catch (Exception e) { }
  86. return null;
  87. }
  88. private static void copyStream(InputStream is, OutputStream os) {
  89. final int buffer_size = 1024;
  90. try {
  91. byte[] bytes = new byte[buffer_size];
  92. for (;;) {
  93. int count = is.read(bytes, 0, buffer_size);
  94. if (count == -1)
  95. break;
  96. os.write(bytes, 0, count);
  97. }
  98. } catch (Exception ex) {
  99. ex.printStackTrace();
  100. }
  101. }
  102. }