经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android判断com.android.camera.action.CROP是否存在
来源:cnblogs  作者:xing_star  时间:2019/11/6 14:08:10  对本文有异议

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

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

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

全局检测的方式:

  1. 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是支持的,也是一种办法

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

  1. private void crop(String imagePath) {
  2. File file = new File(FileUtils.createRootPath(this) + "/" + System.currentTimeMillis() + ".jpg");
  3. cropImagePath = file.getAbsolutePath();
  4. Intent intent = new Intent("com.android.camera.action.CROP");
  5. intent.setDataAndType(getImageContentUri(new File(imagePath)), "image/*");
  6. intent.putExtra("crop", "true");
  7. intent.putExtra("aspectX", config.aspectX);
  8. intent.putExtra("aspectY", config.aspectY);
  9. intent.putExtra("outputX", config.outputX);
  10. intent.putExtra("outputY", config.outputY);
  11. intent.putExtra("scale", true);
  12. intent.putExtra("scaleUpIfNeeded", true);
  13. intent.putExtra("return-data", false);
  14. intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
  15. intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
  16. intent.putExtra("noFaceDetection", true);
  17. startActivityForResult(intent, IMAGE_CROP_CODE);
  18. }

 

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

  1. private boolean canCrop(String imagePath) {
  2. File file = new File(FileUtils.createRootPath(this) + "/" + System.currentTimeMillis() + ".jpg");
  3. Intent intent = new Intent("com.android.camera.action.CROP");
  4. intent.setDataAndType(getImageContentUri(new File(imagePath)), "image/*");
  5. intent.putExtra("crop", "true");
  6. intent.putExtra("aspectX", config.aspectX);
  7. intent.putExtra("aspectY", config.aspectY);
  8. intent.putExtra("outputX", config.outputX);
  9. intent.putExtra("outputY", config.outputY);
  10. intent.putExtra("scale", true);
  11. intent.putExtra("scaleUpIfNeeded", true);
  12. intent.putExtra("return-data", false);
  13. intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
  14. intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
  15. intent.putExtra("noFaceDetection", true);
  16. if (intent.resolveActivity(getPackageManager()) != null) {
  17. return true;
  18. } else {
  19. // 没有安装所需应用
  20. return false;
  21. }
  22. }

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

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

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

 

原文链接:http://www.cnblogs.com/xing-star/p/11803731.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号