/data/data 是本地存储

/storage/ 是外部存储

SD卡存储


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

   public void writeToSdCard() throws IOException {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, permissions, REQUEST_CODE);
        } else {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                String name = et_of_name.getText().toString();
                String content = et_of_content.getText().toString();
                String dir = getExternalFilesDir(null).getAbsolutePath();
                dir = dir + "/" + name;
                FileOutputStream fos = new FileOutputStream(dir);
                fos.write(content.getBytes("utf-8"));
                fos.close();
                Toast.makeText(this, "save to external sdcard", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "without external sdcard", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE:
                if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    try {
                        writeToSdCard();
                    } catch (IOException e) {
                        Toast.makeText(this, "failed save to external sdcard", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(this, "Write External SDCARD Permission Not Granted", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }
    }
    public void read(View v) throws IOException {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            String name = et_of_name.getText().toString();
            String dir = getExternalFilesDir(null).getAbsolutePath();
            dir = dir + "/" + name + "txt";
            InputStream fis = new FileInputStream(dir);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int len = -1;
            byte[] bytes = new byte[1024];
            while ((len = fis.read(bytes)) != -1) {
                bos.write(bytes);
            }
            String content = bos.toString();
            et_of_content.setText(content);
        } else {
            Toast.makeText(this, "without external sdcard", Toast.LENGTH_SHORT).show();
        }
    }

判断文件夹是否存在
File file = new File(dir);if(!file.exists()) {    file.mkdir();}
 
String dir =
 context.getExternalFilesDir(null).getAbsolutePath();

/storage/emulated/0/Android/data/com.example.storage/files

Environment.getExternalStorageDirectory().getAbsolutePath()

/storage/emulated/0

向sd卡读写数据的更多相关文章

  1. Android中向SD卡读写数据,读SD卡和手机内存

    package com.example.sdoperation; import java.io.BufferedReader; import java.io.File; import java.io. ...

  2. 第36章 SDIO—SD卡读写测试

    第36章     SDIO—SD卡读写测试 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/f ...

  3. 第36章 SDIO—SD卡读写测试—零死角玩转STM32-F429系列

    第36章     SDIO—SD卡读写测试 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/f ...

  4. SD卡读写之FileNotFoundException: /storage/emulated/0object.txt: open failed: ENOENT (No such file or dir

    读写sd卡中的文件依照例如以下步骤:1调用Environment的getExternalStorageState()方法推断手机上是否插入了sd卡.而且应用程序具有读写SD卡的能力 //假设手机已经插 ...

  5. 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(八)-认识内存管理

    [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ...

  6. 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(七)-准备移植FatFs

    [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ...

  7. 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(六)-FatFs使用的思路介绍

    [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ...

  8. 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡

    由于一张SD卡要能读写,涉及到的技术有些多,我打算分以下几篇博客 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含 ...

  9. 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍

    其他链接 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 ...

随机推荐

  1. tomcat 项目迁移到jboss5.1

    1.拷贝tomcat\bin目录下两个jar到项目lib 目的:解决jboss java.lang.NoClassDefFoundError: org/apache/juli/logging/LogF ...

  2. Intellij从无到有创建项目

    Intellij虽然提供了很多模板可以创建maven web javaee等等各种项目,但是你知道项目从无到有到底怎么来的,各个配置分别是做什么的?现在就来一步步说明. 1.idea打开一个空文件夹: ...

  3. Wireshark如何选择多行

    Wireshark如何选择多行   在Wireshark中,用户经常需要选择几行,然后进行批量操作,如导出或者分析.但Wireshark没有提供通过鼠标直接选择多行的功能.这个时候,用户需要采用标记分 ...

  4. POJ 3107 Godfather (树重心)

    题目链接:http://poj.org/problem?id=3107 题意: 数重心,并按从小到大输出. 思路: dfs #include <iostream> #include < ...

  5. linux jar 命令使用

    原文链接:http://blog.chinaunix.net/uid-692788-id-2681136.html JAR包是Java中所特有一种压缩文档,其实大家就可以把它理解为.zip包.当然也是 ...

  6. Maven的构建/测试/打包

    继上一篇http://www.cnblogs.com/EasonJim/p/6809882.html使用Maven创建工程后,接下来是使用Maven进行构建/测试/打包. 在打包之前,先熟悉一下Mav ...

  7. 随笔:Golang 时间Time

    先了解下time类型: type Time struct { // sec gives the number of seconds elapsed since // January 1, year 1 ...

  8. zip 压缩文件夹

    import java.io.*; import java.util.zip.*; /** * @author Dana·Li * <p> * 程序实现了ZIP压缩[compression ...

  9. 【翻译自mos文章】使用asmcmd命令在本地和远程 asm 实例之间 拷贝asm file的方法

    使用asmcmd命令在本地和远程 asm 实例之间 拷贝asm file的方法 參考原文: How to Copy asm files between remote ASM instances usi ...

  10. mt-checklist 的 bug 解疑 及 防止 this 指针偏移

    1.今天在使用 mt-checklist 时,发现 绑定 change 方法后,第一次点击返回的值为 空数组 <template> <div id="app"&g ...