• Android-SDCard外部存储文件读写


    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    一想到Android中到SdCard就要想到 Environment,写入到路径一定要用Environment,读取到路径一定要要用Environment,因为每部手机到SdCard到路径都会不同Environment.getExternalStorageDirectory()

    千万不要这样写路径,例如:/mnt/sdcard/ /sdcard/storage/    等等

    SdCard读文件读写操作:

    package liudeli.datastorage;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Environment;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class SdcardActivity extends Activity implements View.OnClickListener {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sdcard);
    
            initViewListener();
        }
    
        private void initViewListener() {
            Button btCheckMountState = findViewById(R.id.bt_check_mount_state);
            Button btOutput = findViewById(R.id.bt_output_sdcard);
            Button btInput = findViewById(R.id.bt_input_sdcard);
    
            btCheckMountState.setOnClickListener(this);
            btOutput.setOnClickListener(this);
            btInput.setOnClickListener(this);
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.bt_check_mount_state:
                    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                        alterToast("Sdcard可用");
                    } else {
                        alterToast("Sdcard不可用");
                    }
                    break;
                case R.id.bt_input_sdcard: {
                    File file = new File(Environment.getExternalStorageDirectory(), "MySdcard.txt");
                    if (!file.exists()) {
                        alterToast("Sdcard的文件不存在");
                        return;
                    }
                    try {
                        BufferedReader br = new BufferedReader(new FileReader(file));
                        String result = br.readLine();
                        alterToast(result);
                        br.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                }
                case R.id.bt_output_sdcard: {
                    File file = new File(Environment.getExternalStorageDirectory(), "MySdcard.txt");
                    if (!file.exists()) {
                        try {
                            file.createNewFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    try {
                        FileWriter fileWriter = new FileWriter(file);
                        fileWriter.write("饭撒发生的噶地方舒服舒服撒冯绍峰撒");
                        fileWriter.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
                }
                default:
                    break;
            }
        }
    
        private void alterToast(String text) {
            Toast.makeText(SdcardActivity.this, text, Toast.LENGTH_LONG).show();
        }
    }

    Layout Code:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/bt_check_mount_state"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="检查SDCard挂载状态"/>
    
        <Button
            android:id="@+id/bt_output_sdcard"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="写入到Sdcard文件"
            />
    
        <Button
            android:id="@+id/bt_input_sdcard"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="读取Sdcard文件"
            />
    
    </LinearLayout>
  • 相关阅读:
    VS使用技巧
    写的一个简单定时器(非独立线程)
    C/C++技巧
    【转载】R6034错误,C Runtime Error
    C/C++面试题(一)
    常用的coco2d-x游戏开发工具(转)
    AndroidJNI 调用JAVA(转)
    Android SDK +Eclipse+ADT+CDT+NDK 开发环境在windows 7下的搭建
    简单的字符串压缩--C代码
    SQLite: sqlite_master(转)
  • 原文地址:https://www.cnblogs.com/android-deli/p/10088515.html
Copyright © 2020-2023  润新知