android中的AlertDailogBox,正在保存文件
在我的应用程序中,单击按钮,我会显示警报日志框,以获取文件名。并从用户处获取文件名。然后保存它。 但现在发生的是,点击按钮后,alert dailog框位于屏幕前面,在此之前,它尝试保存文件。但它尝试在用户输入文件名之前保存文件,这就是为什么保存文件时文件名为空。如何调用use alert框,以便我可以从用户处获取名称,然后使用该名称保存文件。请帮助我android中的AlertDailogBox,正在保存文件,android,file,android-alertdialog,Android,File,Android Alertdialog,在我的应用程序中,单击按钮,我会显示警报日志框,以获取文件名。并从用户处获取文件名。然后保存它。 但现在发生的是,点击按钮后,alert dailog框位于屏幕前面,在此之前,它尝试保存文件。但它尝试在用户输入文件名之前保存文件,这就是为什么保存文件时文件名为空。如何调用use alert框,以便我可以从用户处获取名称,然后使用该名称保存文件。请帮助我 public void savebitmap(Bitmap bitmap) { str++; AlertDialog.Buil
public void savebitmap(Bitmap bitmap)
{
str++;
AlertDialog.Builder alert = new AlertDialog.Builder(Work.this);
alert.setMessage("File name :");
input = new EditText(Work.this);
input.setLayoutParams(new LayoutParams(100,50));
alert.setView(input);
alert.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
NameValue = input.getText().toString();
System.out.println(" file name.---"+NameValue);
}
});
alert.show();
System.out.println("file is..."+NameValue);
try
{
System.out.println("in bitmap save...");
File fn=new File("/sdcard/"+" filename4"+".png");
FileOutputStream out=new FileOutputStream(fn);
System.out.println(",,,,,,,,,,,,,,,"+out);
Toast.makeText(getApplicationContext(), "In Save",Toast.LENGTH_SHORT).show();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90,out);
out.flush();
out.close();
Toast.makeText(getApplicationContext(), "File is Saved in "+fn, Toast.LENGTH_SHORT).show();
}
catch(Exception e){
e.printStackTrace();
}
}
您需要先获取输入,然后当用户按OK时,您需要创建文件。所以您需要在side
onClick
中编写代码
试试这个
public void savebitmap(Bitmap bitmap)
{
str++;
AlertDialog.Builder alert = new AlertDialog.Builder(Work.this);
alert.setMessage("File name :");
input = new EditText(Work.this);
input.setLayoutParams(new LayoutParams(100,50));
alert.setView(input);
alert.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
NameValue = input.getText().toString();
System.out.println(" file name.---"+NameValue);
try
{
System.out.println("in bitmap save...");
File fn=new File("/sdcard/"+" filename4"+".png");
FileOutputStream out=new FileOutputStream(fn);
System.out.println(",,,,,,,,,,,,,,,"+out);
Toast.makeText(getApplicationContext(), "In Save",Toast.LENGTH_SHORT).show();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90,out);
out.flush();
out.close();
}
catch(Exception e){
e.printStackTrace();
}
}
});
alert.show();
System.out.println("file is..."+NameValue);
}
您需要将所有文件保存代码移动到按钮的onClick事件中,或者将所有代码包装到一个方法中,然后在按钮单击时调用它,如下所示:
public void savebitmap(Bitmap bitmap){
//....your code here
alert.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
NameValue = input.getText().toString();
System.out.println(" file name.---"+NameValue);
// put your code here to save file on Ok button click
saveFileOnSdCard(NameValue);
}
});
alert.show();
}
private void saveFileOnSdCard(String str_filename){
// move yor file saving code here..
}