我想創建一個服務來上傳文件列表在後臺。當我定期檢查MultipleFileUpload對象時,它始終處於TransferState WAITING狀態,並且上傳的字節數和要上載的總字節數始終爲0.在服務中運行TransferManager還是以這種方式定期檢查MultipleFileUpload時是否存在某些問題?Amazone s3上傳使用傳輸管理器似乎永遠不會停止等待
我已經在手機和模擬器上測試過。我以前能夠在使用PutObjectRequest而不是TransferManager時成功上傳相同的AmazonS3Client。
這裏是到目前爲止的代碼:
// OnCreate
@Override
public void onCreate() {
Log.v(TAG, "Creating Upload Service");
ClientConfiguration clientConfig=new ClientConfiguration();
clientConfig.setConnectionTimeout(10*1000);
AmazonS3Client s3Client = new AmazonS3Client(
new BasicAWSCredentials(ACCESS_KEY_ID,
SECRET_KEY),clientConfig);
s3Client.setEndpoint(AMAZON_ENDPOINT);
transferManager= new TransferManager(s3Client);
seekIntent = new Intent(BROADCAST_ACTION);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Insert notification start
initNotification();
String[] fileNames=intent.getExtras().getStringArray(MainActivity.UPLOAD_FILE_NAMES);
String path=intent.getExtras().getString(MainActivity.UPLOAD_FILE_PATH);
ArrayList<File> files=new ArrayList<File>();
for (String fileName : fileNames) {
files.add(new File(fileName));
}
upload= transferManager.uploadFileList(RECORDING_BUCKET, null,new File(path),files);
seekIntent.putExtra(UPLOAD_BYTES_TOTAL_KEY, upload.getProgress().getTotalBytesToTransfer());
transferManager.
setupHandler();
return START_STICKY;
}
// ---Send seekbar info to activity----
private void setupHandler() {
handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 1000); // 1 second
}
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
// // Log.d(TAG, "entered sendUpdatesToUI");
seekIntent.putExtra(UPLOAD_BYTES_TRANSFERED_KEY, upload.getProgress().getBytesTransfered());
if(upload.isDone())
{
int retCode=(upload.getState()==TransferState.Completed)?0:1;
seekIntent.putExtra(UPLOAD_COMPLETION_CODE_KEY, retCode);
}
else
{
handler.postDelayed(this, 1000);
}
sendBroadcast(seekIntent);
}
};