我正在开发一款应用,允许用户使用Facebook SDK 4在其时间轴上上传照片。 当时,我在Facebook上上传了照片"我做了进度对话的草稿。
问题在于这两件事不能合作。进度条不显示实际进度百分比。
我的问题是:我怎样才能"附加" progressDialog到"在Facebook上传照片"?
代码分享图片。
private void sharePostOnFacebook() {
Bitmap image = BitmapFactory.decodeFile(mCurrentPhotoPath);
SharePhoto sharePhoto = new SharePhoto.Builder()
.setBitmap(image)
.setCaption(mPostContent)
.build();
SharePhotoContent content = new SharePhotoContent.Builder()
.addPhoto(sharePhoto)
.build();
// Check if the user already has publish permission
if (hasPublishPermission()) {
ShareApi.share(content, shareCallback);
showProgress();
} else {
List<String> publishPermission = Arrays.asList("publish_actions");
LoginManager.getInstance().logInWithPublishPermissions(this, publishPermission);
sharePostOnFacebook();
}
}
这是回调方法,用于管理ShareApi.share
的结果。
private FacebookCallback<Sharer.Result> shareCallback = new FacebookCallback<Sharer.Result>() {
@Override
public void onCancel() {
progress.dismiss();
}
@Override
public void onError(FacebookException error) {
progress.dismiss();
String title = getString(R.string.fb_share_error);
String alertMessage = error.getMessage();
showResult(title, alertMessage);
}
@Override
public void onSuccess(Sharer.Result result) {
progress.dismiss();
if (result.getPostId() != null) {
String title = getString(R.string.fb_share_success);
String id = result.getPostId();
String alertMessage = getString(R.string.successfully_posted_post, id);
showResult(title, alertMessage);
}
}
private void showResult(String title, String alertMessage) {
new AlertDialog.Builder(getActivity())
.setTitle(title)
.setMessage(alertMessage)
.setPositiveButton(R.string.fb_share_ok, null)
.show();
}
};
最后,创建 progressDialog的showProgess()
方法。
private void showProgress() {
progress = new ProgressDialog(getContext());
progress.setCancelable(true);
progress.setMessage("Upload Photo");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.setProgress(0);
progress.show();
final int totalProgressTime = 100;
final Thread thread = new Thread() {
@Override
public void run() {
int jumpTime = 0;
while (jumpTime < totalProgressTime) {
try {
sleep(200);
jumpTime += 5;
progress.setProgress(jumpTime);
}
catch (InterruptedException exc) {
exc.printStackTrace();
}
}
}
};
thread.start();
}