Android 获取drawable目录图片 并存入指定文件的步骤详解

作者:路过火车 时间:2021-11-01 07:04:48 

第一步:获取存储的路径 我们用/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();
}

来源:https://blog.csdn.net/weixin_42362496/article/details/105157631

标签:android,目录图片,存入,文件
0
投稿

猜你喜欢

  • javac -encoding 用法详解

    2022-06-28 08:58:08
  • C#中函数的创建和闭包的理解

    2022-08-17 01:14:35
  • Android调试华为和魅族手机logcat不显示的问题

    2023-12-06 04:17:55
  • java实现马踏棋盘算法(骑士周游问题)

    2022-03-17 20:29:46
  • Springmvc模式上传和下载与enctype对比

    2022-11-08 09:14:17
  • 一文搞懂Java创建线程的五种方法

    2023-10-30 18:35:04
  • java使用集合实现通讯录功能

    2023-01-30 21:27:15
  • Java如何获取word文档的条目化内容

    2023-10-27 15:04:32
  • SpringBoot使用ApplicationEvent&Listener完成业务解耦

    2021-10-19 06:36:41
  • 如何设置Spring Boot测试时的日志级别

    2023-11-10 14:11:20
  • 讲解.NET环境下绘制模糊数学中隶属函数分布图第1/5页

    2022-10-04 10:21:51
  • 详解SpringCloud的负载均衡

    2022-03-14 03:42:28
  • ELK搭建线上日志收集系统

    2021-11-01 17:34:41
  • C# dynamic关键字的使用方法

    2023-02-26 08:40:01
  • java 算法之希尔排序详解及实现代码

    2022-07-12 23:09:45
  • 使用Files.walkFileTree遍历目录文件

    2021-09-27 06:12:40
  • 关于C#连接FTP时路径问题的解决方法

    2021-10-03 18:53:50
  • 深入了解Java对象的克隆

    2021-10-29 13:59:35
  • Eclipse下基于Java的OpenCV开发环境配置教程

    2022-01-30 21:02:54
  • Java集合使用 Iterator 删除元素

    2022-02-25 12:32:44
  • asp之家 软件编程 m.aspxhome.com