版权声明:本文为xing_star原创文章,转载请注明出处!

本文同步自http://javaexception.com/archives/225

最近线上报错,有个用户连续crash了10次左右,查看了下堆栈信息,发现是提示com.android.camera.action.CROP这个Intent找不到,报了ActivityNotFound的错误。根据经验得出结论,这个用户的设备上,肯定是去掉了支持Crop的应用,所以直接做Intent隐私跳转到这会crash,思考了下,解决思路是在跳转前做检测,或者是全局做检测。

全局检测的方式:

public boolean isAvailable(Context context, Intent intent) { 
  PackageManager packageManager = context.getPackageManager();
List list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
  return list.size() > 0;
}

经过测试,在com.android.camera.action.CROP没效果,只能放弃,但是这个对某些Intent是支持的,也是一种办法

第二种就是在跳转前检测:

private void crop(String imagePath) {
File file = new File(FileUtils.createRootPath(this) + "/" + System.currentTimeMillis() + ".jpg"); cropImagePath = file.getAbsolutePath();
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(getImageContentUri(new File(imagePath)), "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", config.aspectX);
intent.putExtra("aspectY", config.aspectY);
intent.putExtra("outputX", config.outputX);
intent.putExtra("outputY", config.outputY);
intent.putExtra("scale", true);
intent.putExtra("scaleUpIfNeeded", true);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, IMAGE_CROP_CODE);
}

我修改后的检测代码如下:

private boolean canCrop(String imagePath) {
File file = new File(FileUtils.createRootPath(this) + "/" + System.currentTimeMillis() + ".jpg");
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(getImageContentUri(new File(imagePath)), "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", config.aspectX);
intent.putExtra("aspectY", config.aspectY);
intent.putExtra("outputX", config.outputX);
intent.putExtra("outputY", config.outputY);
intent.putExtra("scale", true);
intent.putExtra("scaleUpIfNeeded", true);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);
if (intent.resolveActivity(getPackageManager()) != null) {
return true;
} else {
// 没有安装所需应用
return false;
}
}

关键代码是Intent.resolveActivity(getPackageManager()) != null

推荐一篇值得看看的文章:

https://juejin.im/entry/59a93530f265da24823642b3

最新文章

  1. 基于Angular2的前端框架CoreUI开发使用说明
  2. python中协程的使用示例
  3. Android TextView走马灯效果
  4. scala 学习笔记(06) OOP(下)多重继承 及 AOP
  5. 记一个dynamic的坑
  6. linux内核更新前后配置文件的比较
  7. [流媒体]VLC主要模块
  8. php session偶尔写入失败的原因
  9. javascript 基础学习整理 二 之 html对象总结,参考W3C
  10. Android(java)学习笔记253:ContentProvider使用之内容观察者02
  11. 同ListView该接口无法通过手势滑动左右切换界面问题解决方法
  12. Java单例模式的各种实现(饿汉、懒汉、静态内部类、static代码块、enum枚举类型)
  13. Jquery 改变样式
  14. 【笔记】HybridApp中使用Promise化的JS-Bridge
  15. SQL 高效运行注意事项(一)
  16. 面试中程序员常见的Redis"刁难"问题,值得一读!
  17. 2018年中国C++大会详细日程+报名
  18. Aviutl 视频处理软件
  19. Integer的最大值
  20. mysql导入数据方法和报错解决

热门文章

  1. 防范XSS攻击
  2. Spring 框架基础(05):事务管理机制,和实现方式
  3. scrapy请求传参-BOSS反爬
  4. LNMP Shell脚本发布
  5. 谈谈.net对象生命周期
  6. 学习索引结构的一些案例——Jeff Dean在SystemML会议上发布的论文(下)
  7. 华为云北京四业务,访问北京一OBS桶,配置指南
  8. Selenium 4 Java的最佳测试框架
  9. 宜信SDL实践:产品经理如何驱动产品安全建设
  10. UVA-11107 Life Forms(求出现K次的子串,后缀数组+二分答案)