第一步:获取存储的路径 我们用/sdcard/Android/data/包名/的路径 方便我们测试查看

 String path=MyApplication.getContextObject().getExternalFilesDir("").toString(); File file=new File(path);

第二步:根据该文件中存储的路径信息在文件系统上创建一个新的空文件

File finalImageFile = new File(file, System.currentTimeMillis() + ".jpg"); try {   finalImageFile.createNewFile(); } catch (IOException e) {   e.printStackTrace(); }

第三步:将字节放入文件输出流

FileOutputStream fos = null; try {   fos = new FileOutputStream(finalImageFile); } catch (FileNotFoundException e) {   e.printStackTrace(); }

第四步:将图片压缩成图片格式

 BitmapDrawable bitmapDrawable = (BitmapDrawable)MyApplication.getContextObject().getResources().getDrawable(R.drawable.account); Bitmap bitmap=bitmapDrawable.getBitmap(); if (bitmap == null) {   Toast.makeText(MyApplication.getContextObject(), "图片不存在",Toast.LENGTH_LONG).show();   return; } bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); try {   fos.flush();   fos.close();   Toast.makeText(MyApplication.getContextObject(), "图片保存在:"+ finalImageFile.getAbsolutePath(), Toast.LENGTH_LONG).show(); } catch (IOException e) {   e.printStackTrace(); }

完整代码

 String path=MyApplication.getContextObject().getExternalFilesDir("").toString(); File file=new File(path);  File finalImageFile = new File(file, System.currentTimeMillis() + ".jpg"); try {   finalImageFile.createNewFile(); } catch (IOException e) {   e.printStackTrace(); }  FileOutputStream fos = null; try {   fos = new FileOutputStream(finalImageFile); } catch (FileNotFoundException e) {   e.printStackTrace(); }  BitmapDrawable bitmapDrawable = (BitmapDrawable)MyApplication.getContextObject().getResources().getDrawable(R.drawable.account); Bitmap bitmap=bitmapDrawable.getBitmap(); if (bitmap == null) {   Toast.makeText(MyApplication.getContextObject(), "图片不存在",Toast.LENGTH_LONG).show();   return; } bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); try {   fos.flush();   fos.close();   Toast.makeText(MyApplication.getContextObject(), "图片保存在:"+ finalImageFile.getAbsolutePath(), Toast.LENGTH_LONG).show(); } catch (IOException e) {   e.printStackTrace(); }

总结

到此这篇关于Android 获取drawable目录图片 并存入指定文件的文章就介绍到这了,更多相关android 目录图片存入指定文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

更多相关文章

  1. 用python开发android应用 【转载】
  2. Android版本管理(git 和 repo)
  3. android apk 应用 重新签名 方法
  4. 四、android studio使用自己库编译工程
  5. 安卓android模拟器使用命令行
  6. Android(安卓)系统开发生成签名供 Android(安卓)Studio 使用
  7. Android之如何解决右上角不显示3个点的菜单
  8. 安卓按钮有按下去的效果的实现方法
  9. Android多渠道打包修改资源文件

随机推荐

  1. python入门教程13-03 (python语法入门之表
  2. 【DB宝39】使用Docker分分钟搭建Zabbix 5
  3. 【DB宝19】在Docker中使用MySQL高可用之M
  4. 【DB宝40】MySQL高可用管理工具Orchestra
  5. 【微信小程序】swiper轮播图
  6. 【uni-app/微信小程序】预览图片(单张、多
  7. 【js知识】splice()实现数组的添加、删除
  8. 【微信小程序】tab选项卡
  9. 【js知识】DOM获取元素的方法
  10. 20210202-1 大数据云计算介绍和基础(上)