安卓开发涉及到图片的各种处理时,不论是压缩还是上传文件,经常会用到drawable、bitmap、file类型的转换。
drawable转bitmap:
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.pic);
file转bitmap:
File mFile = new File();
Bitmap mBitmap = BitmapFactory.decodeFile(mFile.getPath());
文件大小表示:mFile .length()
private static String FormetFileSize(long fileS) {
DecimalFormat df = new DecimalFormat("#.00");
String fileSizeString = "";
String wrongSize = "0B";
if (fileS == 0) {
return wrongSize;
}
if (fileS < 1024) {
fileSizeString = df.format((double) fileS) + "B";
} else if (fileS < 1048576) {
fileSizeString = df.format((double) fileS / 1024) + "KB";
} else if (fileS < 1073741824) {
fileSizeString = df.format((double) fileS / 1048576) + "MB";
} else {
fileSizeString = df.format((double) fileS / 1073741824) + "GB";
}
return fileSizeString;
}
url转bitmap:
开启子线程,建立连接请求,下载图片。
Bitmap bitmap;
public Bitmap returnBitMap(final String url){
new Thread(new Runnable() {
@Override
public void run() {
URL imageurl = null;
try {
imageurl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection)imageurl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
return bitmap;
}
bitmap转file:
private String SAVE_PIC_PATH = Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)
? Environment.getExternalStorageDirectory().getAbsolutePath() : "/mnt/sdcard";
private String SAVE_REAL_PATH = SAVE_PIC_PATH + "/zachary/savePic";
//保存方法
private void saveFile(Bitmap bm, String fileName) throws IOException {
String subForder = SAVE_REAL_PATH;
File foder = new File(subForder);
if (!foder.exists()) foder.mkdirs();
File myCaptureFile = new File(subForder, fileName);
if (!myCaptureFile.exists()) myCaptureFile.createNewFile();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
//发送广播通知系统
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(myCaptureFile);
intent.setData(uri);
this.sendBroadcast(intent);
}
bitmap转byte:
private byte[] Bitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
byte转bitmap:
private Bitmap Bytes2Bimap(byte[] b) {
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
} else {
return null;
}
}
使用compressor进行图片压缩:
1.引用:
// 图片压缩
implementation 'id.zelory:compressor:2.1.0'
2.使用
转化为File:
File compressedImageFile = new Compressor(this).compressToFile(actualImageFile);
转化为Bitmap:
Bitmap compressedImageBitmap = new Compressor(this).compressToBitmap(actualImageFile);
3.自定义压缩:
File compressedImage = new Compressor(this)
.setMaxWidth(640)
.setMaxHeight(480)
.setQuality(75)
.setCompressFormat(Bitmap.CompressFormat.WEBP)
.setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getAbsolutePath())
.compressToFile(actualImage);
4.配合RxJava:
new Compressor(this)
.compressToFileAsFlowable(actualImage)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<File>() {
@Override
public void accept(File file) {
compressedImage = file;
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) {
throwable.printStackTrace();
showError(throwable.getMessage());
}
});