在我的应用程序中,有一个按钮可以将图片上传到 Facebook。按下此按钮时,会出现一个警报框以获取用户标题,然后上传图片。现在,如果我在将图片上传到 facebook 时按 BACK 按钮(在给出并OK
按下标题后),那么我会再次看到我的活动,但这次当我再次尝试上传图片时,我没有看到警告框(尽管它处于隐形模式,因为如果我点击OK
按钮的位置,然后图片就会上传。这里发生了什么?
//Listener to button to upload to facebook
class ButtonListener3 implements View.OnClickListener{
@Override
public void onClick(View v) {
Pic.this.Commentbox();
}
}
public void Commentbox(){
value="";
alert1 = new AlertDialog.Builder(this);
alert1.setTitle("Write caption");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert1.setView(input);
alert1.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
value = input.getText().toString();
Pic.this.fb();
}
});
alert1.setNegativeButton("No, thanks", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
value="";
Pic.this.fb();
}
});
alert1.show();
}
public void fb(){
final Facebook facebook=new Facebook(ID);
facebook.authorize(Pic.this, new String[] { "publish_stream" },
new DialogListener() {
@Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
}
@Override
public void onError(DialogError dialogError) {
// TODO Auto-generated method stub
}
@Override
public void onComplete(Bundle values) {
postToWall(values.getString(Facebook.TOKEN));
}
private void postToWall(String accessToken) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] data = baos.toByteArray();
Bundle bundle = new Bundle();
bundle.putString(Facebook.TOKEN, accessToken);
bundle.putByteArray("facebookPictureData", data);
// The byte array is the data of a picture.
bundle.putByteArray("picture", getIntent().getExtras().getByteArray("data"));
bundle.putString("caption",value);
try {
facebook.request("me/photos", bundle, "POST");
Toast.makeText(getApplicationContext(),"Picture uploaded to your facebook account successfully",
Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException fileNotFoundException) {
Toast.makeText(getApplicationContext(),"Picture upload failed, try again",
Toast.LENGTH_SHORT).show();
} catch (MalformedURLException malformedURLException) {
Toast.makeText(getApplicationContext(),"Picture upload failed, try again",
Toast.LENGTH_SHORT).show();
} catch (IOException ioException) {
Toast.makeText(getApplicationContext(),"Picture upload failed, try again",
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
}
});
}