`
xiang37
  • 浏览: 431135 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Android文件读写包含SDCard

阅读更多

 

package com.xiva.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class FileActivity extends Activity {
	
	private EditText fileName;
	private EditText content;
	private TextView textContent;
	private static final String TAG = "FileActivity";
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        fileName = (EditText) this.findViewById(R.id.fileName);
        content  = (EditText) this.findViewById(R.id.content);
        textContent = (TextView) this.findViewById(R.id.textContent);
        Button saveBtn = (Button)this.findViewById(R.id.saveButton);
        Button viewBtn = (Button)this.findViewById(R.id.viewButton);
        
        saveBtn.setOnClickListener(listener);//保存按钮事件监听
        viewBtn.setOnClickListener(listener);//查看按钮事件监听
    }
    
    private View.OnClickListener listener = new View.OnClickListener(){
    	    	
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Button button     = (Button) v;
			String nameStr    = fileName.getText().toString().trim();
	        String contentStr = content.getText().toString();
	        boolean isExsit   = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
	        switch(button.getId())
	        {
	        	case R.id.saveButton :
	    	        int resId_s             = R.string.success;
	    	        FileOutputStream fileOS = null;
	    			try {
//	    				Context.MODE_APPEND----已append的模式保存文件内容,
//	    				将现在的内容追加到原有文件内容之后
//	    				Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,
//	    				只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
//	    				Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE
//	    				用来控制其他应用是否有权限读写该文件
		
	    				//如若SD卡存在,文件则存入SD卡
	    				if(isExsit)
	    				{
	    					fileOS = null;
	    					File file     = new File(Environment.getExternalStorageDirectory(), nameStr+".txt");
	    					fileOS        = new FileOutputStream(file);
	    				}
	    				else
	    				{
		    				fileOS  = FileActivity.this.openFileOutput(nameStr+".txt",
		    						Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE + Context.MODE_APPEND);
	    					Toast.makeText(FileActivity.this, "SDCard不存在或者处于保护状态,文件写入手机内存", Toast.LENGTH_LONG).show();
	    				}
	    				FileService.save(fileOS, contentStr);
	    			} catch (Exception e) {
	    				resId_s = R.string.failure;
	    				e.printStackTrace();
	    			}
//	    			界面提示
	    			Toast.makeText(FileActivity.this, resId_s, Toast.LENGTH_LONG).show();
	    			Log.i(TAG, nameStr);
	    			Log.i(TAG, contentStr);
	    			break;
	        	case R.id.viewButton :
	        		int resId_v 	   = R.string.readSucc;
	        		InputStream fileIS = null;
	        		String contentSt   = null;
					try {
						if(isExsit)
						{
							File file = new File(Environment.getExternalStorageDirectory(), nameStr+".txt");
							fileIS    = new FileInputStream(file);
						}
						else
						{
							fileIS    = FileActivity.this.openFileInput(nameStr+".txt");
						}
						contentSt = FileService.read(fileIS);
					} catch (Exception e) {
						resId_v = R.string.readFail;
						e.printStackTrace();
					}
					textContent.setText(contentSt);
					Log.i(TAG, contentSt);
					Toast.makeText(FileActivity.this, resId_v, Toast.LENGTH_LONG).show();
					break;
				default: break;
	        }

		}
    	   	
    };
    
}
 

 

	<!-- 在SDCard中创建与删除文件权限 -->
	<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
	<!-- 往SDCard写入数据权限 -->
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

 

 

在配置文件中的数据权限加入,完整代码见附件!

总结:写入手机内存的文件,权限由程序控制,加入为private,则只可 允许本应用访问;而文件在SDCard上的存储,其权限由配置文件,也就是由Android系统根据配置来告知应用!注意,在对SDCard进行操作前,先对其存在性进行判断!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics