Java 简单警报对话框问题-Android
我有一个正在开发的应用程序,可以将图片上传到facebook相册。应用程序上传照片很好,没有问题。如果我硬编码的话,我也可以在照片上加上说明。我试图做的是创建一个警报对话框,捕获用户标题,然后在上传图片之前将其放入捆绑包中。现在发生的是照片上传,然后我得到对话框输入标题 以下是弹出警报对话框的方法Java 简单警报对话框问题-Android,java,android,facebook,android-alertdialog,Java,Android,Facebook,Android Alertdialog,我有一个正在开发的应用程序,可以将图片上传到facebook相册。应用程序上传照片很好,没有问题。如果我硬编码的话,我也可以在照片上加上说明。我试图做的是创建一个警报对话框,捕获用户标题,然后在上传图片之前将其放入捆绑包中。现在发生的是照片上传,然后我得到对话框输入标题 以下是弹出警报对话框的方法 public String createAlert() { AlertDialog.Builder alert = new AlertDialog.Builder(this
public String createAlert() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Enter Caption for Photo");
alert.setMessage("Caption :");
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
imageCaption = input.getText().toString();
return;
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
return;
}
});
AlertDialog helpDialog = alert.create();
helpDialog.show();
return imageCaption;
}
现在,这里是捆绑和上传到facebook
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_EXISTING_PHOTO_RESULT_CODE: {
if (resultCode == RESULT_OK){
Uri photoUri = data.getData();
String imagePath = getPath(photoUri);
byte[] data1 = null;
Bitmap bi = BitmapFactory.decodeFile(imagePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data1 = baos.toByteArray();
Bundle params = new Bundle();
params.putString(Facebook.TOKEN, facebook.getAccessToken());
params.putString("caption", createAlert() );
params.putByteArray("photo", data1);
try {
facebook.request("me/photos",params,"POST");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
}
default: {
facebook.authorizeCallback(requestCode, resultCode, data);
break;
}
}
}
在createAlert方法完成时,用户尚未输入任何内容。显示对话框的事件刚刚添加到系统消息中,一旦代码停止运行,系统消息将在将来处理。然后,你的代码继续在facebook上发布帖子。然后显示对话框。然后,单击正确的内容时,OnClickListener中的代码就会运行 对话操作发生后,您需要发送Facebook帖子。我认为直接的方法是将createAlert方法内联到您现在调用它的行的正上方。然后将Facebook代码的其余部分粘贴到onClick中。这将为您提供正确的顺序,您可以再次将事情分解为方法 只要意识到调用show不会立即执行任何操作,onclick中的代码也不会立即运行。这只是将一个事件排队,并分别指定当另一个事件发生时会发生什么。它是基于事件的编程,就像许多GUI API一样
编辑:我将尝试编辑您的代码,以显示如何使其工作。但是,您没有发布足够的内容来编译它,因此可能需要进行一些清理。在这里:
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_EXISTING_PHOTO_RESULT_CODE:
if ( RESULT_OK == resultCode) {
final Uri photoUri = data.getData();
createAlert(photoUri);
}
break;
default:
facebook.authorizeCallback(requestCode, resultCode, data);
break;
}
}
public void createAlert(final Uri photoUri) {
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Enter Caption for Photo");
alert.setMessage("Caption :");
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int whichButton) {
final String caption = input.getText().toString();
postToFacebook(photoUri, caption);
}
});
alert.setNegativeButton("Cancel", null);
final AlertDialog helpDialog = alert.create();
helpDialog.show();
}
private void postToFacebook(final Uri photoUri, final String caption) {
final String imagePath = getPath(photoUri);
final byte[] data1 = null;
Bitmap bi = BitmapFactory.decodeFile(imagePath);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data1 = baos.toByteArray();
bi.recycle();
bi = null;
final Bundle params = new Bundle();
params.putString(Facebook.TOKEN, facebook.getAccessToken());
params.putString("caption", caption);
params.putByteArray("photo", data1);
try {
facebook.request("me/photos", params, "POST");
} catch (final FileNotFoundException e) {
e.printStackTrace();
} catch (final MalformedURLException e) {
e.printStackTrace();
} catch (final IOException e) {
e.printStackTrace();
}
}
如果您需要查看完整的代码,请访问:我不确定我是否正确理解您所说的内容。我将尝试编辑您的代码,以演示如何使其工作。但是,您没有发布足够的内容来编译它,因此可能需要进行一些清理。见上文答案中增加的新部分。