标签:create log 图像 ssd 方式 OLE media extern 防止
获取bitmap的两种方式
//第一种方式:从资源文件中得到图片
Bitmap rawBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.haha);
//第二种方式:从SD卡中得到图片(方法1)
String SDCarePath=Environment.getExternalStorageDirectory().toString();
String filePath=SDCarePath+"/"+"haha.jpg";
Bitmap rawBitmap1 = BitmapFactory.decodeFile(filePath, null);
//将设置图片的圆角
Bitmap roundCornerBitmap=this.toRoundCorner(rawBitmap, 40);
imageView.setImageBitmap(roundCornerBitmap);
/**
* @param bitmap 需要修改的图片
* @param pixels 圆角的弧度
* @return 圆角图片
*/
//参考资料:
//http://blog.csdn.net/c8822882/article/details/6906768
public Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
Bitmap roundCornerBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(roundCornerBitmap);
int color = 0xff424242;//int color = 0xff424242;
Paint paint = new Paint();
paint.setColor(color);
//防止锯齿
paint.setAntiAlias(true);
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
RectF rectF = new RectF(rect);
float roundPx = pixels;
//相当于清屏
canvas.drawARGB(0, 0, 0, 0);
//先画了一个带圆角的矩形
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
//再把原来的bitmap画到现在的bitmap!!!注意这个理解
canvas.drawBitmap(bitmap, rect, rect, paint);
return roundCornerBitmap;
}
//——————————————————————————————————————————————————————————压缩且保存图片到SDCard
private void compressAndSaveBitmapToSDCard(Bitmap rawBitmap,String fileName,int quality){
String saveFilePaht=this.getSDCardPath()+File.separator+fileName;
File saveFile=new File(saveFilePaht);
if (!saveFile.exists()) {
try {
saveFile.createNewFile();
FileOutputStream fileOutputStream=new FileOutputStream(saveFile);
if (fileOutputStream!=null) {
//imageBitmap.compress(format, quality, stream);
//把位图的压缩信息写入到一个指定的输出流中
//第一个参数format为压缩的格式
//第二个参数quality为图像压缩比的值,0-100.0 意味着小尺寸压缩,100意味着高质量压缩
//第三个参数stream为输出流
rawBitmap.compress(Bitmap.CompressFormat.JPEG, quality, fileOutputStream);
}
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//获取SDCard的目录路径功能
private String getSDCardPath() {
String SDCardPath = null;
// 判断SDCard是否存在
boolean IsSDcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if (IsSDcardExist) {
SDCardPath = Environment.getExternalStorageDirectory().toString();
}
return SDCardPath;
}
//————————————————————————————————————以上为压缩且保存图片到SDCard
//————>以下为获取SD卡图片的缩略图方法
String SDCarePath2=Environment.getExternalStorageDirectory().toString();
String filePath2=SDCarePath2+"/"+"haha.jpg";
Bitmap tempBitmap=BitmapFactory.decodeFile(filePath2);
Bitmap bitmapThumbnail2=ThumbnailUtils.extractThumbnail(tempBitmap, 100, 100);
imageView.setImageBitmap(bitmapThumbnail2);
//————>以上为获取SD卡图片的缩略图方法
转自:https://blog.csdn.net/daoren1/article/details/51344085
标签:create log 图像 ssd 方式 OLE media extern 防止
原文地址:https://www.cnblogs.com/kazix/p/13168189.html