1. 图片的二次采样,避免图片太大OOM public class MainActivity extends AppCompatActivity { private in
public class MainActivity extends AppCompatActivity {
private int MY_PERMISSioNS_REQUEST_CALL_PHONE=1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//得到手机宽高
wm=(WindowManager) getSystemService(WINDOW_SERVICE);
WindowHeight=wm.getDefaultDisplay().getHeight();
WindowWidth=wm.getDefaultDisplay().getWidth();
iv1=(ImageView) findViewById(R.id.iv1);
// 权限申请
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},MY_PERMISSIONS_REQUEST_CALL_PHONE);
} else
{
}
}
// 权限回调
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE)
{
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
} else
{
// Permission Denied
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
return;
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
// 避免图片太大挂了
// 默认情况下 每个 anroid 给应用分配最大 堆大小16M (VM heap)
public void sendNotification(View view) {
// 设置为true,那么不解析图片,获取图片部分信息
BitmapFactory.Options opts=new BitmapFactory.Options();
// 如果这里设置为true,那么就不为图片分配内存,而是起到对图片测量的公共
// 查看bitmap信息,获取图片的头部信息
opts.inJustDecodeBounds=true;
BitmapFactory.decodeFile("/sdcard/1.jpg",opts);
int imageHeight=opts.outHeight;
int imageWidth=opts.outWidth;
Log.e("Tag", "图片的高:"+imageHeight);
Log.e("Tag", "图片的宽:"+imageWidth);
// 得到手机屏幕的宽高
int scaleX=imageWidth/WindowWidth;
int scaleY=imageHeight/WindowHeight;
int scale=1;
// 按照大的来缩放,避免显示到屏幕外面
if(scaleX>scaleY && scaleY>=1){
scale=scaleX;
}
if(scaleY>scaleX && scaleX>=1){
scale=scaleY;
}
// 解析图片
opts.inJustDecodeBounds=false;
// 采样率
opts.inSampleSize=scale;
Bitmap bitmap=BitmapFactory.decodeFile("/sdcard/1.jpg",opts);
iv1.setImageBitmap(bitmap);
}
}
图片头信息读取api:
通过api:ExifInterface
比如拍摄时间
ExifInterface exif= null;
try {
exif = new ExifInterface("/sdcard/1.jpg");
} catch (IOException e) {
Log.e("Tag", "图片异常");
e.printStackTrace();
}
String time=exif.getAttribute(ExifInterface.TAG_DATETIME); //通过getAttribute()获取照片的属性:时间、
Log.e("Tag", "拍摄时间:"+time);
String type=exif.getAttribute(ExifInterface.TAG_MODEL);
String location = exif.getAttribute(ExifInterface.TAG_GPS_LONgitUDE);
Log.e("Tag", "相机是:"+type);
Log.e("Tag", "拍摄地点:"+location);
--结束END--
本文标题: Android 图片优化
本文链接: https://lsjlt.com/news/29386.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0