涉及到的内容 :
1、图片剪切,2、图片保存到SD卡 3、文件读取和保存 4、自定义View 绘制平滑曲线
5、RecycleView 显示 6、Glide框架显示图片 7、大图片显示 8、从相册选择图片
所用设备:
三星 华为 平板
目录结构
Utils封装
1.权限检查
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
public class CheckPermissionsUtil {
private static final String TAG = " fuck";
Context mContext;
public CheckPermissionsUtil(Context mContext) {
this.mContext = mContext;
}
private String[] needPermissions = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
private boolean checkPermission(String... needPermissions) {
for (String permission : needPermissions) {
if (ActivityCompat.checkSelfPermission(mContext, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
private void requestPermission(Activity activity, int code, String... needPermissions) {
ActivityCompat.requestPermissions(activity, needPermissions, code);
Log.i(TAG, "request Permission...");
}
public void requestAllPermission(Activity activity) {
if (Build.VERSION.SDK_INT >= 23) {
Log.i(TAG, "request All Permission...");
for (String permission : needPermissions) {
if (!checkPermission(permission)) {
requestPermission(activity, 0, permission);
}
}
}
}
}
调用流程
CheckPermissionsUtil checkPermissionsUtil = new CheckPermissionsUtil(this);
checkPermissionsUtil.requestAllPermission(this);
2.调用系统图片剪切
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
/**
* Created by Administrator on 2017/5/5 0005.
* 图片裁剪
*/
public class CutUitl {
public static Uri Ucrop(Uri uri, Activity activity) {
// 裁剪图片
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 800);
intent.putExtra("aspectY", 1000);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("FaceDetection", true); // face detection
activity.startActivityForResult(intent, 200);
return uri;
}
public static void UcropNoUri(Uri uri, Activity activity) {
// 裁剪图片
if(null == uri){
return;
}
Intent intent = new Intent();
intent.setAction("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");// mUri是已经选择的图片Uri
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 800);
intent.putExtra("aspectY", 1000);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
activity.startActivityForResult(intent, 200);
}
}
3.文件读取和存储
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import cn.forward.androids.utils.ImageUtils;
/**
* 文件读取和保存
*/
public class FileUtil {
/*存储过的文件名路径*/
private static String SAVEPATH = "/sdcard/DCIM/savealva/";
private static final String SAVEFILENAME = "save.txt";
/*存储对应坐标和文件*/
private static String PATH = "/sdcard/DCIM/alva/";
private static final String TAG = "zhangxin";
private static String strContent;
private static File files = null;
private static File file = null;
private static String strFilePath;
public static void writeTxtToFile(String strcontent,String name,String path) {
//生成文件夹之后,再生成文件,不然会出错
makeFilePath(path, name);
strFilePath = path + name;
// 每次写入时,都换行写
strContent = strcontent + "\r\n";
try {
File file = new File(strFilePath);
if (!file.exists()) {
Log.e(TAG, "输出信息-->: " + strFilePath);
file.getParentFile().mkdirs();
file.createNewFile();
}
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
raf.seek(file.length());
raf.write(strContent.getBytes());
raf.close();
} catch (Exception e) {
Log.e(TAG, "输出信息-->: " + e.getMessage());
}
}
/**
* @param fileName
* @return
* @throws IOException
* @desc 读取sd卡文件内容
*/
public static String readSDCard(String fileName) throws IOException {
// File file = new File(Environment.getExternalStorageDirectory(),fileName);
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
byte[] data = bos.toByteArray();
fis.close();
bos.close();
return new String(data);
}
// 生成文件
public static File makeFilePath(String filePath, String fileName) {
File file = null;
makeRootDirectory(filePath);
try {
file = new File(filePath + fileName);
if (!file.exists()) {
file.createNewFile();
}
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
// 生成文件夹
public static void makeRootDirectory(String filePath) {
File file = null;
try {
file = new File(filePath);
if (!file.exists()) {
file.mkdir();
}
} catch (Exception e) {
Log.i("error:", e + "");
}
}
//吐司
public static void Toast(Context context, String str) {
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
//保存图片
public static void SaveImage(String savePath, boolean isDir, String name, Bitmap bitmap, Context context) {
File files = null;
File file = null;
if (TextUtils.isEmpty(savePath)) {
File dcimFile = new File(Environment.getExternalStorageDirectory(), "Alva");
files = new File(dcimFile, "zx");
// 保存的路径
// file = new File(files, System.currentTimeMillis() + ".jpg");
file = new File(files, name);
} else {
if (isDir) {
files = new File(savePath);
// 保存的路径
//file = new File(files, System.currentTimeMillis() + ".jpg");
file = new File(files, name);
} else {
file = new File(savePath);
files = file.getParentFile();
}
}
files.mkdirs();
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
ImageUtils.addImage(context.getContentResolver(), file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
}
}
}
}
}