1.以R文件的方式获取,即从…/main/res/drawable/a.jpg中获取图片对应的bitmap
第一种:
Bitmap bp = BitmapFactory.decodeResource(this.getBaseContext().getResources(), R.drawable.a);
第二种:
BitmapDrawable bitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.a);
Bitmap bitmap = bitmapDrawable.getBitmap();
第三种:
Bitmap bp=BitmapFactory.decodeStream(getClass().getResourceAsStream("/res/drawable/a.jpg"));
2.通过uri(网页或本地地址)获取bitmap,
第一种:
Uri mImageCaptureUri = data.getData();
Bitmap photoBmp = null;
if (mImageCaptureUri != null) {
photoBmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mImageCaptureUri);
}
第二种:
bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri));
第三种(获取过程中以2为除数对宽高进行缩放):
private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException
{
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE =500;//700
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);
}
3.通过String类型的photopath获取bitmap,
private Bitmap getDiskBitmap(String pathString){
Bitmap bitmap = null;
try{
File file=new File(pathString);
if (file.exists()){
bitmap= BitmapFactory.decodeFile(pathString);
}
}catch (Exception e){
e.printStackTrace();
}
return bitmap;
}
4.通过框架Glide获取并显示
ImageView iv_photo;
String photoPath;
private File cameraSavePath;
cameraSavePath = new File(Environment.getExternalStorageDirectory().getPath() + "/" + System.currentTimeMillis() +".jpg");
photoPath = String.valueOf(cameraSavePath);
Glide.with(this).load(photoPath).apply(RequestOptions.noTransformation()
.override(iv_photo.getWidth(),iv_photo.getHeight())
.error(R.drawable.default_person_icon))
.into(iv_photo);
第二种:
https://blog.csdn.net/wl724120268/article/details/79552196
第三种:
http://www.cppcns.com/ruanjian/android/213221.html