Android向SD卡写入文件

举报
yd_221104950 发表于 2020/12/02 23:42:05 2020/12/02
【摘要】 1、检查是否有读写sdcard的权限 (1)首先要在AndroidManifest.xml加入 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WR...

1、检查是否有读写sdcard的权限

(1)首先要在AndroidManifest.xml加入

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

  
 
  • 1
  • 2

如果AndroidManifest.xml没有加入上面两条权限,是不可能进行动态权限申请的。动态权限申请,需要先在AndroidManifest.xml配置相应的权限。
(2)动态申请SD卡读写权限

 private void checkWriteAndReadPermission() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED || checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) { String[] permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}; requestPermissions(permissions, 1000); } } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); for (int permission : grantResults) { if (permission == PackageManager.PERMISSION_DENIED) { Toast.makeText(this, "申请权限失败", Toast.LENGTH_LONG).show(); break; } } }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

2、创建文件目录

 private boolean createDir(String dir) { File fileDir = new File(dir); if (fileDir.exists() && fileDir.isDirectory()) { return true; } else { return fileDir.mkdirs(); } }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3、创建文件

 private File createFile(String fileName) { File file = new File(fileDir, fileName); if (file.exists() && file.isFile()) { return file; } else { try { if (file.createNewFile()) { return file; } else { return null; } } catch (IOException e) { e.printStackTrace(); return null; } } }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

4、向文件写入数据

 private void write2File(File file, String data) { OutputStream ou = null; try { ou = new FileOutputStream(file); byte[] buffer = data.getBytes(); ou.write(buffer); ou.flush(); Toast.makeText(this,"写入成功",Toast.LENGTH_LONG).show(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (ou != null) { ou.close(); } } catch (Exception e) { e.printStackTrace(); } } }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

整个过程大概如此,Demo在Github上,欢迎下载学习

谢谢阅读!

文章来源: blog.csdn.net,作者:WongKyunban,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_40763897/article/details/100132792

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200