视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存"
android:textColor="@color/black"
android:textSize="@dimen/common_font_size" />
<Button
android:id="@+id/btn_read"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取"
android:textColor="@color/black"
android:textSize="@dimen/common_font_size" />
<ImageView
android:id="@+id/iv_content"
android:layout_width="match_parent"
android:layout_height="400dp"
android:scaleType="fitCenter" />
</LinearLayout>
对应java文件:ImageWriteActivity.java
- 获取组件id,设置点击事件
- onClick方法中使用switch判断。
- 如果点击保存,则设置图片名为当前时间(毫秒级)加后缀。然后用getExternalFilesDir()方法获取当前APP的私有下载目录。再从指定资源文件(这里是drawable)中获取位图对象。最后调用工具类中的saveImage方法把位图对象保存为图片文件。
- 如果点击读取,则调用工具类中的openImage方法,然后把位图赋值给ImageView组件。
package com.example.chapter06;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import com.example.chapter06.util.FileUtil;
import com.example.chapter06.util.ToastUtil;
import java.io.File;
public class ImageWriteActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView iv_content;
private String path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_write);
iv_content = findViewById(R.id.iv_content);
findViewById(R.id.btn_save).setOnClickListener(this);
findViewById(R.id.btn_read).setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_save:
String fileName = System.currentTimeMillis() + ".jpeg";
// 获取当前APP的私有下载目录
path = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + File.pathSeparator + fileName;
Log.d("ning", path);
// 从指定的资源文件中获取位图对象
Bitmap b1 = BitmapFactory.decodeResource(getResources(),R.drawable.ceshi1);
// 把位图对象保存为图片文件
FileUtil.saveImage(path,b1);
ToastUtil.show(this,"保存成功");
break;
case R.id.btn_read:
Bitmap b2 = FileUtil.openImage(path);
iv_content.setImageBitmap(b2);
break;
}
}
}
还有一个工具类:FileUtil.java
package com.example.chapter06.util;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileUtil {
// 把位图数据保存到指定路径的图片文件
public static void saveImage(String path, Bitmap bitmap) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(path);
// 把位图数据压缩到文件输入流中
bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos);
}catch (Exception e) {
e.printStackTrace();
}finally {
if (fos != null) {
try {
fos.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
// 从指定路径的图片文件中读取位图数据
public static Bitmap openImage(String path) {
Bitmap bitmap = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(path);
bitmap = BitmapFactory.decodeStream(fis);
}catch (Exception e) {
e.printStackTrace();
}finally {
if (fis != null) {
try {
fis.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
return bitmap;
}
}
定义了保存方法和读取方法供上一个文件调用
点击保存后,打印出图片信息
2022-08-03 15:32:02.722 5961-5961/com.example.chapter06 D/ning: /storage/emulated/0/Android/data/com.example.chapter06/files/Download:1659519122721.jpeg
2022-08-03 15:32:02.807 5961-5993/com.example.chapter06 D/EGL_emulation: eglMakeCurrent: 0xdc91a360: ver 3 0 (tinfo 0xdc90f1e0)
2022-08-03 15:32:02.813 5961-5993/com.example.chapter06 D/EGL_emulation: eglMakeCurrent: 0xdc91a360: ver 3 0 (tinfo 0xdc90f1e0)
图片保存在了这个路径里(我保存了五次才有五个):
点击读取,将图片显示在ImageView组件中