版权声明:本文为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

Android判断com.android.camera.action.CROP是否存在的更多相关文章

  1. 解决图片裁剪com.android.camera.action.CROP和intent.putExtra("return-data", true);

    最近在做一个图片上传,在上传之前需要对照片进行裁剪,遇到一个坑,在别的手机上运行都正常,在小米手机上却遇见一个问题,选中图片无法裁剪,直接闪退,目前已解决!之前出过问题的地方会标红 //选择图片 pr ...

  2. 注意android裁图的Intent action

    现在很多开发者在裁图的时候还是使用com.android.camera.action.CROP 来调用 startActivity(). 这不是个好主意. 任何不是依android开头的Action ...

  3. Android判断Touch为滑动事件还是操作控件

    Android判断Touch为滑动事件还是操作控件 因为在项目中要判断WebView是否处于滚动状态,但它不像ListView有onScrollStateChanged方法来监听,要实现就得手动监听它 ...

  4. Android开发学习之Camera

    今天本来想写一篇关于百度地图定位SDK的文章的,无奈根据官网提供的例子编写的程序始终无法运行,所以这个计划只能落空.那么今天要与大家分享的是Camera,即照相机.随着硬件能力的大幅提升,手机上各种依 ...

  5. Android判断网络是否打开,并打开设置网络界面

    由于Android的SDK版本不同所以里面的API和设置方式也是有少量变化的,尤其是在Android 3.0 及后面的版本,UI和显示方式也发生了变化,现在就以打开网络设置为例,同大家分享一下: 1. ...

  6. android判断adb调试是否打开及代码跳转到开发者选项界面

    boolean enableAdb = (Settings.Secure.getInt(getContentResolver(), Settings.Secure.ADB_ENABLED, 0) &g ...

  7. Android使用MediaRecorder和Camera实现视频录制及播放功能整理

    转载请注明出处:http://blog.csdn.net/woshizisezise/article/details/51878566 这两天产品经理向我丢来一个新需求,需要在项目里添加一个视频录制的 ...

  8. Android 判断一个 View 是否可见 getLocalVisibleRect(rect) 与 getGlobalVisibleRect(rect)

    Android 判断一个 View 是否可见 getLocalVisibleRect(rect) 与 getGlobalVisibleRect(rect) [TOC] 这两个方法的区别 View.ge ...

  9. Android判断GPS是否开启和强制帮用户打开GPS

    引子:在我们的应用为用户提供定位服务时,通常想为用户提供精确点的定位服务,这是需要用户配合的.我们必须先检测用户手机的GPS当前是否打开,若没打开则弹出对话框提示.用户若不配合我们也没办法,只能采用基 ...

随机推荐

  1. Slickflow.Graph 开源工作流引擎快速入门之四: 图形编码建模工具使用手册

    前言: 业务人员绘制流程时,通常使用图形GUI界面交互操作来完成,然而对于需要频繁操作或者管理较多流程的系统管理用户,就需要一款辅助工具,来帮助他们快速完成流程的创建和编辑更新.Slickflow.G ...

  2. kubeadm join 超时报错 error execution phase kubelet-start: error uploading crisocket: timed out waiting for the condition

    解决: swapoff -a kubeadm reset systemctl daemon-reload systemctl restart kubelet iptables -F && ...

  3. vsftpd cmds_allowed 权限控制

    vsftpd cmds_allowed cmds_allowed=ABOR,CWD,LIST,MDTM,MKD,NLST, PASS,PASV,PORT,PWD,QUIT,RETR,RMD,RNFR, ...

  4. 你真的了解foreach吗?

    引言 有C#基础的,当问到循环有哪些,会毫不犹豫的说出的for.do while.foreach及while这几种,但是到具体实际开发中,我们遇到一些问题,比如:到底选择哪种?为什么选择这种?哪种好像 ...

  5. WebGPU学习(三):MSAA

    大家好,本文学习MSAA以及在WebGPU中的实现. 上一篇博文 WebGPU学习(二): 学习"绘制一个三角形"示例 下一篇博文 WebGPU学习(四):Alpha To Cov ...

  6. 源码分析 RocketMQ DLedger 多副本之 Leader 选主

    目录 1.DLedger关于选主的核心类图 1.1 DLedgerConfig 1.2 MemberState 1.3 raft协议相关 1.4 DLedgerRpcService 1.5 DLedg ...

  7. 使用PaintCode便捷地实现动画效果

    // // ViewController.m // paintCodeTestOC //gif // Created by LongMa on 2019/7/25. // #import " ...

  8. Apache ServiceComb 开源两周年,聊聊其与微服务的前世今生

    欢迎添加华为云小助手微信(微信号:HWCloud002 或 HWCloud003),输入关键字"加群",加入华为云线上技术讨论群:输入关键字"最新活动",获取华 ...

  9. Python的Requests库基本方法函数

    一.Requests 库的七个常用函数: 1. requests.request(method,url,**kwargs) :method:请求方式,对应get/put/post等七种 :拟获取页面的 ...

  10. 有了 serverless,前端也可以快速开发一个 Puppeteer 网页截图服务

    更多云原生技术资讯可关注阿里巴巴云原生技术圈. Puppeteer 是什么? puppeteer 官网的介绍如下: Puppeteer is a Node library which provides ...