aws SDK を使用して一連の画像をアップロードしています。各リスト項目に ProgressBar(Style:Horizontal) を含む ListView があり、アップロードされたファイルに基づいて進行状況を設定する必要があります。
aws SDK ( http://aws.amazon.com/releasenotes/Java/3538638977238478 ) の TransferManager を使用して、アップロードされたデータを取得しました。
私はこれを試しましたが、うまくいきません:
public class SyncFragment extends Fragment {
ListView uploadList;
public UploadAdapter uploadAdapter;
public ArrayList<QueueItem> queueList = new ArrayList<QueueItem>();
AWSCredentials myCredentials = new BasicAWSCredentials(Constants.ACCESS_KEY_ID, Constants.SECRET_KEY);
TransferManager tm = new TransferManager(myCredentials);
// Empty cons as per documentation of Fragments
public SyncFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);
// Set the queue list
queueList = ((NSApplication) getActivity().getApplication()).queueList;
// Init Adapter
uploadAdapter = new UploadAdapter(getActivity());
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.upload_list, container, false);
uploadList = (ListView) view.findViewById(R.id.uploadList);
uploadList.setAdapter(uploadAdapter);
return view;
}
class UploadAdapter extends ArrayAdapter<QueueItem> {
private LayoutInflater layoutInflater;
private BitmapFromId bitmapFromId;
QueueItem item;
public UploadAdapter(Context context) {
super(context, 0);
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
bitmapFromId = new BitmapFromId(context);
}
@Override
public int getCount() {
// Set the total list item count
return queueList.size();
}
@Override
public QueueItem getItem(int position) {
return queueList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.listitem_upload, null);
holder = new ViewHolder();
holder.albumName = (TextView) convertView.findViewById(R.id.albumTitle);
holder.thumb = (ImageView) convertView.findViewById(R.id.thumb);
holder.progressBar = (ProgressBar) convertView.findViewById(R.id.progressbar_Horizontal);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
item = getItem(position);
item.progress = 0;
// load bitmap from id
bitmapFromId.DisplayImage(Long.parseLong(item.itemId), holder.thumb);
holder.albumName.setText(item.itemName);
S3PutObjectTask task = new S3PutObjectTask();
task.execute(item.itemActualPath, item.itemId);
holder.progressBar.setProgress(item.progress);
return convertView;
}
private class S3PutObjectTask extends AsyncTask<String, Integer, S3TaskResult> {
int progress;
protected void onPreExecute() {
progress = 0;
}
protected S3TaskResult doInBackground(String... params) {
S3TaskResult result = new S3TaskResult();
// The file location of the image selected.
String imagePath = params[0];
// Put the image data into S3.
try {
File image = new java.io.File(imagePath);
Transfer myUpload = tm.upload(Constants.getPictureBucket(), params[1] + ".jpg", image);
while (myUpload.isDone() == false) {
progress = (int) (myUpload.getProgress().getBytesTransfered() * 100 / image.length());
publishProgress(progress);
}
myUpload.waitForCompletion();
} catch (Exception exception) {
result.setErrorMessage(exception.getMessage());
}
return result;
}
@Override
protected void onProgressUpdate(Integer... values) {
item.progress = values[0];
uploadAdapter.notifyDataSetChanged();
}
protected void onPostExecute(S3TaskResult result) {
Toast.makeText(getActivity(), "uploaded sucessfully!", Toast.LENGTH_SHORT).show();
if (result.getErrorMessage() != null) {
Toast.makeText(getActivity(), "upload failed...", Toast.LENGTH_SHORT).show();
}
}
}
public class S3TaskResult {
String errorMessage = null;
Uri uri = null;
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public Uri getUri() {
return uri;
}
public void setUri(Uri uri) {
this.uri = uri;
}
}
}
public static class ViewHolder {
ImageView thumb;
TextView albumName;
ProgressBar progressBar;
}
}