android中的AlertDialog Box,保存文件

时间:2013-03-04 05:03:22

标签: android file alertdialog

在我点击按钮的应用程序中,我显示警告dailog框以获取文件名。从用户获取文件的名称并保存。 但正在发生的是,点击按钮后警告dailog框是在屏幕前面,然后它尝试保存文件。但它试图保存文件befor用户输入文件的名称,这就是为什么文件名在保存文件中为空。如何调用使用警告框,以便我可以从用户获取名称,然后使用该名称保存文件。帮助我。

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

2 个答案:

答案 0 :(得分:1)

您需要首先获取输入,然后当用户按OK时需要创建文件。因此,您需要在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);


}

答案 1 :(得分: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..
    }