Activity:
public class ButtonFiveActivity extends AppCompatActivity { private ImageView imageOne; private ImageView imageTwo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_five); imageOne = findViewById(R.id.imageOne); imageTwo = findViewById(R.id.imageTwo); Bitmap bmp = null; try { bmp = PictureProcessingUtil.revitionImageSize(Environment.getExternalStorageDirectory()+"/文件夹名称/FCEB0E51-6118-497B-B3EE-22F420F91054.jpeg",400,400); PictureProcessingUtil.saveBitmap(bmp,this); } catch (IOException e) { e.printStackTrace(); } imageOne.setImageBitmap(bmp); String userImagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/文件夹名称/FCEB0E51-6118-497B-B3EE-22F420F91054.jpeg"; boolean fileExist = PictureProcessingUtil.fileIsExists(userImagePath); if(fileExist){ imageTwo.setImageBitmap(PictureProcessingUtil.saveBitmapFile(userImagePath)); } } }布局简单,即两个imageView,一个用于显示缩略图,一个显示原图,用于对比。
PictureProcessingUtil:
public class PictureProcessingUtil { //获取缩略图 public static Bitmap revitionImageSize(String path, int maxWidth, int maxHeight) throws IOException { Bitmap bitmap = null; try { BufferedInputStream in = new BufferedInputStream(new FileInputStream( new File(path))); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(in, null, options); in.close(); int i = 0; while (true) { if ((options.outWidth >> i <= maxWidth) && (options.outHeight >> i <= maxHeight)) { in = new BufferedInputStream( new FileInputStream(new File(path))); options.inSampleSize = (int) Math.pow(2.0D, i); options.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeStream(in, null, options); break; } i += 1; } } catch (Exception e) { return null; } return bitmap; } public static Bitmap saveBitmapFile(String filePath) { File file = new File(filePath); return BitmapFactory.decodeFile(file.toString()); } //判断文件是否存在 public static boolean fileIsExists(String strFile) { try { File f = new File(strFile); if (!f.exists()) { return false; } } catch (Exception e) { return false; } return true; } // 图片存放路径 public static String sdCardDir = Environment.getExternalStorageDirectory() + "/文件夹名称/"; /** * 保存图片 * * @param bitmap */ public static void saveBitmap(Bitmap bitmap, Context context) { try { File dirFile = new File(sdCardDir); if (!dirFile.exists()) { //如果不存在,那就建立这个文件夹 dirFile.mkdirs(); } File file = new File(sdCardDir, "FCEB0E51-6118-497B-B3EE-22F420F91054little.jpeg");//加little用于名称区分 FileOutputStream fos = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 把文件插入到系统图库 // try { // MediaStore.Images.Media.insertImage(context.getContentResolver(), // file.getAbsolutePath(), fileName, null); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } // // 通知图库更新 // context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, // Uri.parse("file://" + "/sdcard/namecard/"))); } }