0

在我单击按钮的应用程序中,我显示警报对话框以获取文件名。并从用户那里获取文件名。并保存它。但正在发生的事情是,在单击按钮警报 dailog 框后进入屏幕前面,在此之前它尝试保存文件。但它试图在用户输入文件名之前保存文件,这就是为什么文件名在保存文件中为空的原因。如何调用使用警报框,以便我可以从用户那里获取名称,然后使用该名称保存文件。请帮助我。

 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();        
    }
}
4

2 回答 2

1

您需要先输入,然后当用户按 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);


}
于 2013-03-04T05:07:21.367 回答
1

您将在 Button 的 onClick 事件中移动所有文件保存代码或将所有代码包装在一个方法中,然后在 Button 单击时调用它:

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..
    }
于 2013-03-04T05:08:08.003 回答