我正在尝试创建一项服务以在后台上传文件列表。当我定期检查MultipleFileUpload对象时,它总是处于TransferState WAITING,并且上传的字节数和要上传的总字节数总是0。在服务中运行TransferManager,或者定期检查MultipleFileUpload这种方式是否有问题?
我已经在手机和模拟器上进行了测试。当我使用 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);
}
};