Android 拍照获取缩略图以及完整图片(适配androidN)

Android 拍照获取缩略图以及完整图片(适配androidN),第1张

takePictureIntentresolveActivity(getPackageManager()) != null

在官方文档中有描述:startActivityForResult()方法受到调用resolveActivity()的条件的保护,该方法返回可处理该意图的第一个活动组件,执行此检查很重要,因为如果您使用没有应用程序可以处理的意图调用startActivityForResult(),则您的应用程序将崩溃。所以只要结果不为空,就可以安全的使用意图,大概意思是检测手机中有没有相机。

另外一种检测相机的方法是

required=true 表示要安装该应用,手机必须有摄像头该硬件。要不然不允许安装

鸿洋的博客关于android70 以及 40 拍照封装的处理(点击跳转)

类似介绍比较好的文章推荐-Android 调用系统相机拍照攻略(已适配Android N)

以前在实际项目中使用拍照和从图库中获取时,不知道以何种方式从回调中取得资源,以Bitmap方式还是Uri的形式?如果是使用Bitmap,应该注意些什么,Uri又是一种什么样的格式?有时会出现拍照时回调data数据为空的情况,又该如何定位问题呢?裁剪又是怎样决定方案的?以下将针对这几个问题阐述自己的见解。

在Android中,Intent触发 Camera程序,拍好照片后会返回数据,比如摄像头800万像素,拍出来的尺寸为 3200x2400,占据内存大小=3200 x 2400 x 4bytes / (1024 x 1024) = 30MB 图像设置ARGB_8888一个像素点占据4字节内存,这个尺寸对应的 Bitmap会耗光应用程序的内存,出于安全方面的考虑,Android会给你一个缩略图,比如 160 x 120 px。

Q:为何要返回图缩略?

缩略图是指从onActivityForResullt回调方法中 intent保存的参数。这是因为在启动相机程序拍摄,为了让Bitmap数据能在Activity之间传递,不得不将拍摄后的Bitmap进行压缩再传递,因此通过回调从intent中取得的是缩略图在于拍摄的Bitmap太大,Activity之间Bundle存放的数据不能太大,会导致程序无响应。高清原图是指直接将拍摄的以文件/Uri形式保存到手机。

注:Bitmap实现了Parcelable 接口,所有可以在Activity间使用Intent传递。

Q:使用Bitmap需要注意哪些问题?

1、Android 裁剪 Intent 附加数据的含义

| setExtra | DataType | Desciption | Extra |

|:-------- |:--------:| :------: |

|crop| String | Signals the crop feature | value="true" |

|aspectX|int|Aspect Ratio|裁剪横向比例|

|aspectY|int|Aspect Ratio|裁剪纵向比例|

|outputX|int|width od output created from this intent|输出宽度|

|outputY|int|height od output created from this intent|输出高度|

|scale|boolean|should it scale|是否伸缩|

|return-date|boolean|Return the bitmap with Action-inline-data by using the data|是否返回Bitmap数据|

|data|Parcelable|Bitmap to process, you may provide it a bitmap (not tested)|可设置data为Bitmap或者将相应数据同uri联系起来|

|circleCrop|String|if this string is not null, it will provide some cicular cr||

|MediaStore

EXTRA_OUTPUT("output")|URI|set this URI to a File|输出路径到uri中|

2、裁剪终极方案 — 来源有拍照和图库,可采取的 *** 作有:

3、剪切:

Android调用系统拍照的代码:

创建Intent Intent openCameraIntent = new Intent(MediaStoreACTION_IMAGE_CAPTURE);

String imagePath = EnvironmentgetExternalStorageDirectory() + "/image" ;

设置文件路径 File file = new File(imagePath);

if(!fileexists()){

filemkdirs(); }

设置URI,指定相册拍照后保存的路径,Uri imageUri = UrifromFile(new File(imagePath, "imagejsp"));

openCameraIntentputExtra(MediaStoreEXTRA_OUTPUT, imageUri);

startActivityForResult(openCameraIntent, REQUEST_CODE_TAKING_PICTURES);

拍照完以后,文件就会保存在这个指定的目录下了。Uri 里指定了相机拍照的路径。

从URI获得文件路径:

string myImageUrl = "content://media/external/images/media/";Uri uri = Uriparse(myImageUrl); String[] proj = { MediaStoreImagesMediaDATA }; Cursor actualimagecursor = thisctxmanagedQuery(uri,proj,null,null,null); int actual_image_column_index = actualimagecursorgetColumnIndexOrThrow(MediaStoreImagesMediaDATA); actualimagecursormoveToFirst(); String img_path = actualimagecursorgetString(actual_image_column_index);

File file = new File(img_path);Uri fileUri = UrifromFile(file);

以上就是关于Android 拍照获取缩略图以及完整图片(适配androidN)全部的内容,包括:Android 拍照获取缩略图以及完整图片(适配androidN)、Android拍照、从图库导入以及图片裁剪、一个关于android的问题,我通过下面的方法调用相机,可是怎么可以在拍照结束后获取照片的路径呢等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9622380.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存