光讲理论太单调,配上demo食用本文更佳:Github
首先需要强调的是要在AndroidManifest文件中加入允许读写的语句
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在很多时候我们需要读取到手机本地的图片,当我们要设置头像、用自定义图片进行拼图等情况都需要用到,所以学会读取手机本地图片是很实用的。在《第一行代码》中有读取本地图片并且进行切割的实例,如下
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 640);
intent.putExtra("outputY", 640);
intent.putExtra("scale", true);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageURI);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(intent, SELECT_A_PICTURE);
主要是使用Intent.ACTION_CONTENT的intent进行跳转,然后通过传输参数控制裁剪,这是一个带返回数据的intent,返回后imageURI所对应的文件就是裁剪完成的图片,直接读取就能得到图片。
if (request