public
class
MainActivity
extends
Activity {
private
ImageView
iv
;
private
WindowManager
vm
;
private
int
height
;
private
int
width
;
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.
activity_main
);
iv
=(ImageView) findViewById(R.id.
iv
);
//得到屏幕管理者
vm
=(WindowManager)
this
.getSystemService(
WINDOW_SERVICE
);
Point outSize =
new
Point();
//1.得到屏幕的宽和高 getDefaultDisplay():得到屏幕的分辨率
height
=
vm
.getDefaultDisplay(). getHeight();
width
=
vm
.getDefaultDisplay(). getWidth();
}
/**
* 显示图片的方法
*/
public
void
click(View view){
//2.得到图片的宽和高
BitmapFactory.Options opts=
new
Options();
//Options代表bitmap的参数
//不是去真正的解析bitmap而是去解析bitmap的头信息
opts.
inJustDecodeBounds
=
true
;
BitmapFactory.decodeFile(
"/sdcard/IMG_20130627_183247.jp g"
,opts);
int
imgheight=opts.
outHeight
;
int
imgwidth=opts.
outWidth
;
//3.计算缩放比
int
xscale=imgwidth/
width
;
int
yscale=imgheight/
height
;
int
scale=1;
if
(xscale>yscale){
scale=xscale;
}
else
{
scale=yscale;
}
System.
out
.println(
"手机宽是:"
+
width
+
" 手机高是:"
+
height
);
System.
out
.println(
"图片的宽是:"
+imgwidth+
"图片的高是"
+imgheight);
System.
out
.println(
"宽度比例:"
+xscale+
"高度比例:"
+yscale);
System.
out
.println(
"缩放成功,缩放的比例是:"
+scale);
//设施位图显示比例,按照比例去解析位图
opts.
inSampleSize
=scale;
opts.
inJustDecodeBounds
=
false
;
//去真正的解析bitmap
Bitmap bitmap=BitmapFactory.decodeFile(
"/mnt/sdcard/Pictures/1.jpg"
,opts);
iv
.setImageBitmap(bitma p);
}
/**
* 显示图片信息的方法
*
@param
view
*/
public
void
click2(View view){
try
{
//创建exif
ExifInterface exif=
new
ExifInterface(
"/mnt/sdcard/Pictures/1.jpg"
);
String time = exif.getAttribute(ExifInterface.
TAG_DATETIME
);
String model = exif.getAttribute(ExifInterface.
TAG_MODEL
);
System.
out
.println(
"拍照的时间:"
+time);
System.
out
.println(
"相机:"
+model);
}
catch
(IOException e) {
e.printStackTrace();
}
}
}