Android往SD卡上存储文件

时间:2020-12-02 11:55:22

 

 
 
  1. package cn.umgsai.file;  
  2.  
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8.  
  9. import cn.umgsai.service.FileService;  
  10.  
  11. import android.app.Activity;  
  12. import android.content.Context;  
  13. import android.os.Bundle;  
  14. import android.os.Environment;  
  15. import android.util.Log;  
  16. import android.view.View;  
  17. import android.widget.Button;  
  18. import android.widget.EditText;  
  19. import android.widget.TextView;  
  20. import android.widget.Toast;  
  21.  
  22. public class DataActivity extends Activity {  
  23.     private EditText filenameText;  
  24.     private EditText contentText;  
  25.     private TextView resultView;  
  26.     private static final String TAG = "DataActivity";  
  27.     /** Called when the activity is first created. */ 
  28.     @Override 
  29.         public void onCreate(Bundle savedInstanceState) {  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.main);  
  32.         filenameText = (EditText) this.findViewById(R.id.filename);  
  33.         contentText = (EditText) this.findViewById(R.id.content);  
  34.         resultView = (TextView) this.findViewById(R.id.result);  
  35.         String filename = filenameText.getText().toString();  
  36.         Button button = (Button) this.findViewById(R.id.button);  
  37.         Button showButton = (Button) this.findViewById(R.id.showButton);  
  38.         button.setOnClickListener(listener);  
  39.         showButton.setOnClickListener(listener);  
  40.     }  
  41.       
  42.     private View.OnClickListener listener = new View.OnClickListener() {   
  43.         @Override 
  44.             public void onClick(View v) {  
  45.             Button button = (Button) v;  
  46.             String filename = filenameText.getText().toString();  
  47.             switch(button.getId()){  
  48.             case R.id.button://如果是保存按钮  
  49.                 int resId = R.string.success;  
  50.                   
  51.                 String content = contentText.getText().toString();  
  52.                 File file = new File(Environment.getExternalStorageDirectory(),filename);  
  53.                 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){  
  54.                     try {  
  55.                         //OutputStream outStream = DataActivity.this.openFileOutput(filename, Context.MODE_WORLD_WRITEABLE+Context.MODE_WORLD_READABLE);  
  56.                         //四中操作模式  
  57.                         //Context.MODE_PRIVATE=0 覆盖、私有  
  58.                         //Context.MODE_APPEND=32768追加、私有  
  59.                         //Context.MODE_WORLD_READABLE=1其他的程序可以访问  
  60.                         //Context.MODE_WORLD_WRITEABLE=2  
  61.                         //Environment.getExternalStorageDirectory()==new file("/sdcard")  
  62.                           
  63.                           
  64.                         FileOutputStream outStream = new FileOutputStream(file);  
  65.                         try {  
  66.                             FileService.save(outStream, content);  
  67.                         } catch (Exception e) {  
  68.                             Log.e(TAG, e.toString());  
  69.                             resId = R.string.error;  
  70.                         }  
  71.                     } catch (FileNotFoundException e) {  
  72.                         Log.e(TAG, e.toString());  
  73.                         resId = R.string.error;  
  74.                     }  
  75.                     Toast.makeText(DataActivity.this, resId, Toast.LENGTH_LONG).show();  
  76.                 }  
  77.                 else{  
  78.                     Toast.makeText(DataActivity.this,"SD卡不存在或者写保护", Toast.LENGTH_LONG).show();  
  79.                 }  
  80.                 break;  
  81.             case R.id.showButton://如果是显示按钮  
  82.                 try {  
  83.                     InputStream inStream = DataActivity.this.openFileInput(filename);  
  84.                     String text = FileService.read(inStream);  
  85.                     resultView.setText(text);  
  86.                 } catch (Exception e) {  
  87.                     Log.e(TAG, e.toString());  
  88.                     resId = R.string.error;  
  89.                     Toast.makeText(DataActivity.this"读取失败", Toast.LENGTH_LONG).show();  
  90.                 }  
  91.                   
  92.                 break;  
  93.             }  
  94.         }  
  95.     };  
  96.       
  97. }  

 

本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/702525