第一个小应用:图片浏览器 之 四 读写SD卡

读取SD卡

  1. 权限配置:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText etText;
    private TextView tvShow;
    //Environment.getExternalStorageDirectory();获取sd卡的目录
    private File sdCardPath = Environment.getExternalStorageDirectory();
    private String fileName = "Myfile";
    //操作SD卡需要在AndroidManifest.xml中配置使用权限
//    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
//    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etText = (EditText) findViewById(R.id.etText);
        tvShow = (TextView) findViewById(R.id.tvShow);
        findViewById(R.id.btnWrite).setOnClickListener(this);
        findViewById(R.id.btnRead).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnWrite:
                File file = new File(sdCardPath,fileName);
                //检查是否存在sd卡,可能是你没有配置
                if (!sdCardPath.exists()){
                    Toast.makeText(getApplicationContext(),"当前系统没有SD卡目录",Toast.LENGTH_SHORT).show();
                    return;
                    //如果没有sd卡,下面的动作都不做
                }
                if (!file.exists()){
                    try {
                        file.createNewFile();
                        Toast.makeText(getApplicationContext(),"文件创建成功",Toast.LENGTH_SHORT).show();
                        FileOutputStream fos = new FileOutputStream(file);
                        OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
                        osw.write(etText.getText().toString());
                        Toast.makeText(getApplicationContext(),"文件写入完成",Toast.LENGTH_SHORT).show();
                        etText.setText("");
                        osw.flush();
                        fos.flush();
                        osw.close();
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                break;
            case R.id.btnRead:
                //检查是否存在sd卡,可能是你没有配置
                if (!sdCardPath.exists()){
                    Toast.makeText(getApplicationContext(),"当前系统没有SD卡目录",Toast.LENGTH_SHORT).show();
                    return;
                }
                File fileRead = new File(sdCardPath,fileName);
                if (fileRead.exists()){
                    try {
                        FileInputStream fis = new FileInputStream(fileRead);
                        InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
                        char[] buffer = new char[fis.available()];
                        isr.read(buffer);
                        String strBuf = new String(buffer);
                        tvShow.setText(strBuf);
                        isr.close();
                        fis.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                break;

        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值