pictureselector照片选择器返回选中照片路径。参考:https://blog.csdn.net/yechaoa/article/details/79291552
导包:
implementation 'com.github.LuckSiege.PictureSelector:picture_library:v2.2.3'
函数:
1 PictureSelector.create(MainActivity.this) 2 .openGallery(PictureMimeType.ofImage()) 3 .forResult(PictureConfig.CHOOSE_REQUEST);
阿里云OSS存储对象。
照片上传功能。参考文档:https://help.aliyun.com/document_detail/93939.html?spm=a2c4g.11186623.6.1317.4a6f7bf9MyuDN7 。
导包:
implementation 'com.squareup.okhttp3:okhttp:3.4.1' implementation 'com.aliyun.dpa:oss-android-sdk:2.9.5'
函数:
1 public void uploadImage(final String path, final String name) { 2 3 new Thread(new Runnable() { 4 @Override 5 public void run() { 6 OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider(ACCESS_ID, ACCESS_KEY); 7 ClientConfiguration conf = new ClientConfiguration(); 8 conf.setConnectionTimeout(15 * 1000); // 连接超时,默认15秒 9 conf.setSocketTimeout(15 * 1000); // socket超时,默认15秒 10 conf.setMaxConcurrentRequest(8); // 最大并发请求数,默认5个 11 conf.setMaxErrorRetry(2); // 失败后最大重试次数,默认2次 12 13 // oss为全局变量,OSS_ENDPOINT是一个OSS区域地址 14 oss = new OSSClient(getApplicationContext(), OSS_ENDPOINT, credentialProvider, conf); 15 // 构造上传请求。 16 PutObjectRequest put = new PutObjectRequest("sean1246", name, path); 17 // 设置进度回调函数打印进度条。 18 put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() { 19 @Override 20 public void onProgress(PutObjectRequest request, long currentSize, long totalSize) { 21 Log.d("PutObject", "currentSize: " + currentSize + " totalSize: " + totalSize); 22 } 23 }); 24 // 文件元信息的设置是可选的。 25 // ObjectMetadata metadata = new ObjectMetadata(); 26 // metadata.setContentType("application/octet-stream"); // 设置content-type。 27 // metadata.setContentMD5(BinaryUtil.calculateBase64Md5(uploadFilePath)); // 校验MD5。 28 // put.setMetadata(metadata); 29 try { 30 // meta设置请求头,解决访问图片地址直接下载 31 ObjectMetadata meta = new ObjectMetadata(); 32 meta.setContentType(getContentType(path.substring(path.lastIndexOf(".")))); 33 put.setMetadata(meta); 34 PutObjectResult putResult = oss.putObject(put); 35 Log.d("PutObject", "UploadSuccess"); 36 Log.d("ETag", putResult.getETag()); 37 Log.d("RequestId", putResult.getRequestId()); 38 } catch (ClientException e) { 39 // 本地异常,如网络异常等。 40 e.printStackTrace(); 41 } catch (ServiceException e) { 42 // 服务异常。 43 Log.e("RequestId", e.getRequestId()); 44 Log.e("ErrorCode", e.getErrorCode()); 45 Log.e("HostId", e.getHostId()); 46 Log.e("RawMessage", e.getRawMessage()); 47 } 48 } 49 }).start(); 50 }public static String getContentType(String FilenameExtension) { 51 if (FilenameExtension.equalsIgnoreCase(".bmp")) { 52 return "image/bmp"; 53 } 54 if (FilenameExtension.equalsIgnoreCase(".gif")) { 55 return "image/gif"; 56 } 57 if (FilenameExtension.equalsIgnoreCase(".jpeg") || 58 FilenameExtension.equalsIgnoreCase(".jpg") || 59 FilenameExtension.equalsIgnoreCase(".png")) { 60 return "image/jpg"; 61 } 62 if (FilenameExtension.equalsIgnoreCase(".html")) { 63 return "text/html"; 64 } 65 if (FilenameExtension.equalsIgnoreCase(".txt")) { 66 return "text/plain"; 67 } 68 if (FilenameExtension.equalsIgnoreCase(".vsd")) { 69 return "application/vnd.visio"; 70 } 71 if (FilenameExtension.equalsIgnoreCase(".pptx") || 72 FilenameExtension.equalsIgnoreCase(".ppt")) { 73 return "application/vnd.ms-powerpoint"; 74 } 75 if (FilenameExtension.equalsIgnoreCase(".docx") || 76 FilenameExtension.equalsIgnoreCase(".doc")) { 77 return "application/msword"; 78 } 79 if (FilenameExtension.equalsIgnoreCase(".xml")) { 80 return "text/xml"; 81 } 82 return "image/jpg"; 83 }