zoukankan      html  css  js  c++  java
  • Android往SD卡上存储文件

     public class DataActivity extends Activity {  
        private EditText filenameText;  
        private EditText contentText;  
        private TextView resultView;  
        private static final String TAG = "DataActivity";  
        /** Called when the activity is first created. */ 
        @Override 
            public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.main);  
            filenameText = (EditText) this.findViewById(R.id.filename);  
            contentText = (EditText) this.findViewById(R.id.content);  
            resultView = (TextView) this.findViewById(R.id.result);  
            String filename = filenameText.getText().toString();  
            Button button = (Button) this.findViewById(R.id.button);  
            Button showButton = (Button) this.findViewById(R.id.showButton);  
            button.setOnClickListener(listener);  
            showButton.setOnClickListener(listener);  
        }  
          
        private View.OnClickListener listener = new View.OnClickListener() {   
            @Override 
                public void onClick(View v) {  
                Button button = (Button) v;  
                String filename = filenameText.getText().toString();  
                switch(button.getId()){  
                case R.id.button://如果是保存按钮  
                    int resId = R.string.success;  
                      
                    String content = contentText.getText().toString();  
                    File file = new File(Environment.getExternalStorageDirectory(),filename);  
                    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){  
                        try {  
                            //OutputStream outStream = DataActivity.this.openFileOutput(filename, Context.MODE_WORLD_WRITEABLE+Context.MODE_WORLD_READABLE);  
                            //四中操作模式  
                            //Context.MODE_PRIVATE=0 覆盖、私有  
                            //Context.MODE_APPEND=32768追加、私有  
                            //Context.MODE_WORLD_READABLE=1其他的程序可以访问  
                            //Context.MODE_WORLD_WRITEABLE=2  
                            //Environment.getExternalStorageDirectory()==new file("/sdcard")  
                              
                              
                            FileOutputStream outStream = new FileOutputStream(file);  
                            try {  
                                FileService.save(outStream, content);  
                            } catch (Exception e) {  
                                Log.e(TAG, e.toString());  
                                resId = R.string.error;  
                            }  
                        } catch (FileNotFoundException e) {  
                            Log.e(TAG, e.toString());  
                            resId = R.string.error;  
                        }  
                        Toast.makeText(DataActivity.this, resId, Toast.LENGTH_LONG).show();  
                    }  
                    else{  
                        Toast.makeText(DataActivity.this,"SD卡不存在或者写保护", Toast.LENGTH_LONG).show();  
                    }  
                    break;  
                case R.id.showButton://如果是显示按钮  
                    try {  
                        InputStream inStream = DataActivity.this.openFileInput(filename);  
                        String text = FileService.read(inStream);  
                        resultView.setText(text);  
                    } catch (Exception e) {  
                        Log.e(TAG, e.toString());  
                        resId = R.string.error;  
                        Toast.makeText(DataActivity.this, "读取失败", Toast.LENGTH_LONG).show();  
                    }  
                      
                    break;  
                }  
            }  
        };  
          
    }  
    

      

  • 相关阅读:
    学用MVC4做网站六后台管理:6.1.3管理员修改密码
    学用MVC4做网站六后台管理:6.1.1管理员登录、6.1.2退出
    学用MVC4做网站六后台管理:6.1管理员(续)
    学用MVC4做网站六:后台管理(续)
    SiteCore Experience Analytics-路径分析地图
    sitecore教程路径分析器
    SiteCore Experience Analytics-体验分析
    sitecore系列教程之更改您的个人设置
    sitecore中的两种编辑工具介绍
    Sitecore CMS中如何管理默认字段值
  • 原文地址:https://www.cnblogs.com/umgsai/p/3908248.html
Copyright © 2011-2022 走看看