首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >安卓系统中的AlertDailogBox,保存文件

安卓系统中的AlertDailogBox,保存文件
EN

Stack Overflow用户
提问于 2013-03-04 13:03:22
回答 2查看 134关注 0票数 0

在我的应用程序中,点击按钮后,我会显示警报日志框来获取文件name.and,并从用户处获取文件名称,以便保存file.and。但发生的是,在点击按钮警报日志框是屏幕的前面,在此之前,它尝试保存file.but其尝试保存文件之前,用户输入的文件名,这就是为什么保存file.how中的文件名是null调用使用警报框,以便我可以从用户获取名称,然后使用该name.plase帮助我保存文件。

代码语言:javascript
运行
复制
 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();        
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-04 13:08:08

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

代码语言:javascript
运行
复制
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..
    }
票数 1
EN

Stack Overflow用户

发布于 2013-03-04 13:07:21

你需要首先接受输入,然后当用户按下OK时,你需要创建文件。所以你需要在side onClick中编写代码。

尝尝这个,

代码语言:javascript
运行
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15194886

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档