【问题标题】:Uploading multiple image to AWS server using rxJava使用 rxJava 将多个图像上传到 AWS 服务器
【发布时间】:2020-12-17 01:46:29
【问题描述】:

我正在尝试使用 rxJava 将多个图像上传到 ASW。我找到了一个 kotlin 示例。

Upload multiple images(nearly 100) from Android to Amazon S3?.

我尝试使用 Java 来实现它。但找不到任何合适的方法。谁能帮我解决这个问题?

public void processImageFiles(){
        uploadedImageInfo = new ArrayList<>();

        List<Single<Boolean>> uploadList = new ArrayList<>();

         for(File file : selectedImageList){
            String fileExtension = MimeTypeMap.getFileExtensionFromUrl(file.toString());
            String fileName = Calendar.getInstance().getTimeInMillis() + sharedPref.getSharedPrefData(SharedPref.USER_ID) + "." + fileExtension;
            String uploadedFileLink = "https://" + ApiClient.BUCKET_NAME + "." + ApiClient.AWS_END_POINT + "/" + fileName;
            RecordData recordData =  new RecordData();
            recordData.setUrl(uploadedFileLink);
            recordData.setFileType(fileExtension);
            recordData.setFileName(fileName);
            uploadedImageInfo.add(recordData);
            uploadList.add(uploadToAWS(file, recordData));
        }
         

    }


    public Single<Boolean> uploadToAWS(File imageFile, RecordData data) {

        return Single.create(emitter -> {

            AmazonS3 s3Client = new AmazonS3Client(new BasicAWSCredentials(ApiClient.ACCESS_KEY, ApiClient.SECRET_KEY));
            String uploadLink = ApiClient.BUCKET_NAME;
            s3Client.setEndpoint(ApiClient.AWS_END_POINT);
            s3Client.setRegion(Region.getRegion(Regions.US_EAST_1));
            TransferUtility transferUtility = new TransferUtility(s3Client, mContext);
            TransferObserver transferObserver = transferUtility.upload(uploadLink, data.getFileName(), imageFile, CannedAccessControlList.PublicRead);
            mContext.registerReceiver(TransferNetworkLossHandler.getInstance(mContext), new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

            transferObserver.setTransferListener(new TransferListener() {

                @Override
                public void onStateChanged(int id, TransferState state) {

                    Log.e("onStateChanged", state.name() + "");
                    if (state.name().equals(TransferState.COMPLETED.name())) {
                        emitter.onSuccess(true);
                    } else {
                        emitter.onError(null);
                    }

                }

                @Override

                public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {

                    //Implement the code to handle the file uploaded progress.
                    Log.e("e", bytesCurrent + "");

                }

                @Override

                public void onError(int id, Exception exception) {
                    emitter.onError(exception);

                }

            });
        });
    }

【问题讨论】:

    标签: android rx-java rx-android


    【解决方案1】:

    我已经回复了并行运行多个请求的类似问题。你可以找到他们here

    我可以建议你这个实现:

    public final class RecordData {
        private final String url;
        private final String fileType;
        private final String fileName;
    
        public RecordData(String url, String fileType, String fileName) {
            this.url = url;
            this.fileType = fileType;
            this.fileName = fileName;
        }
    
        public String getUrl() {
            return url;
        }
    
        public String getFileType() {
            return fileType;
        }
    
        public String getFileName() {
            return fileName;
        }
    }
    
    public RecordData toRecord(File file) {
        String fileExtension = MimeTypeMap.getFileExtensionFromUrl(file.toString());
        String fileName = Calendar.getInstance().getTimeInMillis() + sharedPref.getSharedPrefData(SharedPref.USER_ID) + "." + fileExtension;
        String uploadedFileLink = "https://" + ApiClient.BUCKET_NAME + "." + ApiClient.AWS_END_POINT + "/" + fileName;
        return new RecordData(uploadedFileLink, fileExtension, fileName);
    }
    
    public void processImageFiles(List<File> selectedImageList) {
        Observable.fromIterable(selectedImageList)
                .flatMapSingle(file -> uploadToAWS(file, toRecord(file)).subscribeOn(Schedulers.io()))
                .subscribe();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-05
      • 2020-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多