#### Android手机拍照、剪裁,并非那么简单
简书地址:[我的简书--T9的第三个三角](http://www.jianshu.com/u/46cb5df3d852)
- **前言**
项目中,基本都有用户自定义头像或自定义背景的功能,实现方法一般都是调用系统相机--拍照,或者系统相册--选择照片,然后进行剪裁,最终设为头像或背景。
而在Android6.0之后,需要动态获取权限,而且Android7.0之后,无法直接根据拍照返回的URI拿到图片,这是因为从安卓7.0开始,直接使用本地真实路径被认为是不安全的,会抛出FileUriExposedExCeption异常,本文就是基于这个功能去针对Android7.0进行操作。
废话不多说,先把基本的页面建立,先来布局。
- **布局**
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent_black_80"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/black"
android:visibility="visible">
<TextView
android:id="@+id/tv_camera"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:padding="10dp"
android:text="@string/camera"
android:textColor="@color/white"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:padding="10dp"
android:text="@string/picture"
android:textColor="@color/white"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="center"
android:padding="10dp"
android:text="@string/done"
android:textColor="@color/white"
android:textSize="18sp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_camera"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/black">
<com.isseiaoki.simplecropview.CropImageView xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="@+id/iv_wallpaper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
custom:scv_crop_mode="fit_image"
custom:scv_frame_color="@color/white"
custom:scv_frame_stroke_weight="3dp"
custom:scv_guide_color="@color/white"
custom:scv_guide_show_mode="show_on_touch"
custom:scv_guide_stroke_weight="1dp"
custom:scv_handle_color="@color/white"
custom:scv_handle_show_mode="show_always"
custom:scv_handle_size="8dp"
custom:scv_min_frame_size="100dp"
custom:scv_overlay_color="@color/down_fragment_alpha" />
</RelativeLayout>
<LinearLayout
android:id="@+id/ll_contral"
android:layout_width="match_parent"
android:layout_height="90dp"
android:background="@color/white"
android:orientation="horizontal"
android:paddingBottom="30dp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="20dp"
android:visibility="visible">
<TextView
android:id="@+id/tv_reset"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginRight="14dp"
android:layout_weight="2"
android:background="@drawable/reset_selector"
android:gravity="center"
android:text="Reset"
android:textColor="@color/white" />
<ImageView
android:id="@+id/left_rotate"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginRight="14dp"
android:layout_weight="1"
android:background="@drawable/wallpaper_selector"
android:src="@drawable/rotate_left" />
<ImageView
android:id="@+id/right_rotate"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginRight="14dp"
android:layout_weight="1"
android:background="@drawable/wallpaper_selector"
android:src="@drawable/rotate_right" />
<ImageView
android:id="@+id/up_reversal"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginRight="14dp"
android:layout_weight="1"
android:background="@drawable/wallpaper_selector"
android:src="@drawable/reversal_up" />
<ImageView
android:id="@+id/left_reversal"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@drawable/wallpaper_selector"
android:src="@drawable/reversal_left" />
</LinearLayout>
</LinearLayout>```
![布局](http://upload-images.jianshu.io/upload_images/2789715-22589dd6fff685d0?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
布局很简单,点击Camera、Picture、Done,分别调用手机拍照、调用系统相册照片、完成操作。
- **调用相机拍照**
Android6.0之前,调用系统拍照,只需要在AndroidManifest.xml声明
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
权限,而在6.0以后,不仅需要声明,更需要运行时申请权限,先来调用相机拍照。
1.初始化控件,使用ButterKnife,这个简直傻瓜式的完成
```
@BindView(R.id.tv_camera)
TextView tvCamera;
@BindView(R.id.tv_picture)
TextView tvPicture;
@BindView(R.id.tv_done)
TextView tvDone;
@BindView(R.id.toolbar)
RelativeLayout toolbar;
@BindView(R.id.iv_wallpaper)
CropImageView ivWallpaper;
@BindView(R.id.rl_camera)
RelativeLayout rlCamera;
@BindView(R.id.tv_reset)
TextView tvReset;
@BindView(R.id.left_rotate)
ImageView leftRotate;
@BindView(R.id.right_rotate)
ImageView rightRotate;
@BindView(R.id.up_reversal)
ImageView upReversal;
@BindView(R.id.left_reversal)
ImageView leftReversal;
@BindView(R.id.ll_contral)
LinearLayout llContral;
private static final int CAMERA_REQUEST_CODE = 1;
private static final int REQUEST_CAPTURE = 2;
private static final int REQUEST_PICTURE = 5;
private static final int REVERSAL_LEFT = 3;
private static final int REVERSAL_UP = 4;
private Uri imageUri;
private Uri localUri = null;```
2.点击拍照:
```
tvCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkPremission(); //检查权限
}
});
private void checkPremission() {
final String permission = Manifest.permission.CAMERA; //相机权限
final String permission1 = Manifest.permission.WRITE_EXTERNAL_STORAGE; //写入数据权限
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED
没有合适的资源?快使用搜索试试~ 我知道了~
Android调用系统拍照、系统相册、剪裁.zip
共95个文件
png:43个
xml:35个
java:11个
需积分: 5 0 下载量 171 浏览量
2024-03-06
11:11:21
上传
评论
收藏 1.04MB ZIP 举报
温馨提示
【项目资源】: 包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。 包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】: 所有源码都经过严格测试,可以直接运行。 功能在确认正常工作后才上传。 【适用人群】: 适用于希望学习不同技术领域的小白或进阶学习者。 可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】: 项目具有较高的学习借鉴价值,也可直接拿来修改复刻。 对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】: 有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 鼓励下载和使用,并欢迎大家互相学习,共同进步。 # 注意 1. 本资源仅用于开源学习和技术交流。不可商用等,一切后果由使用者承担。 2. 部分字体以及插图等来自网络,若是侵权请联系删除。
资源推荐
资源详情
资源评论
收起资源包目录
Android调用系统拍照、系统相册、剪裁.zip (95个子文件)
资料总结
微信截图_20170901112642.png 17KB
app
src
androidTest
java
com
ddz
lifestyle
ExampleInstrumentedTest.java 738B
main
java
com
ddz
lifestyle
utils
CompareUtil.java 381B
LifeStyle.java 788B
ImageUtils.java 17KB
IOUtil.java 2KB
Constants.java 930B
ConnectionHelper.java 1KB
StringUtils.java 73KB
FileStorage.java 1KB
FileUtils.java 9KB
activity
SkinActivity.java 10KB
res
anim
slide_right_out.xml 407B
slide_right_in.xml 407B
slide_left_in.xml 408B
slide_down_in.xml 409B
slide_left_out.xml 408B
slide_up_out.xml 410B
mipmap-xxhdpi
ic_launcher.png 8KB
mipmap-hdpi
ic_launcher.png 3KB
drawable-xhdpi
folder.png 848B
edit.png 2KB
bad.png 1KB
category.png 463B
download.png 895B
run.gif 359KB
refresh.png 2KB
favorite.png 2KB
splash_bg.png 66KB
atm.png 2KB
search.png 2KB
share.png 1KB
warning.png 2KB
close.png 805B
delete.png 918B
good.png 1KB
register_bg.png 135KB
pen.png 341B
information.png 2KB
all.png 2KB
selected.png 758B
comments.png 1KB
favorites.png 1KB
mipmap-mdpi
ic_launcher.png 2KB
mipmap-xxxhdpi
ic_launcher.png 10KB
mipmap-xhdpi
logo.png 735B
ic_launcher.png 5KB
xml
file_paths.xml 184B
values
colors.xml 714B
strings.xml 2KB
styles.xml 2KB
layout
activity_skin.xml 5KB
drawable
bmob_update_button_cancel_bg_selector.xml 498B
bmob_update_button_ok_bg_focused.xml 303B
frame.xml 453B
bmob_update_button_ok_bg_normal.xml 327B
circul_shape.xml 712B
shape.xml 445B
bmob_update_button_check_selector.xml 2KB
bmob_update_button_ok_bg_selector.xml 492B
weather_shpe.xml 718B
reset_check.xml 359B
bmob_update_dialog_bg.xml 215B
bmob_update_button_ok_bg_tap.xml 274B
bmob_update_button_cancel_bg_tap.xml 274B
navigation_textcolor.xml 270B
bmob_update_button_close_bg_selector.xml 451B
reset_normal.xml 360B
bmob_update_button_cancel_bg_focused.xml 303B
shape_bg.xml 306B
line.xml 288B
bmob_update_wifi_disable.png 3KB
bmob_update_button_cancel_bg_normal.xml 327B
wallpaper_selector.xml 773B
reset_selector.xml 258B
drawable-hdpi
reversal_left.png 1KB
rotate_left.png 2KB
rotate_right.png 2KB
bmob_update_btn_check_on_holo_light.png 1KB
bmob_update_btn_check_off_pressed_holo_light.png 1KB
bmob_update_btn_check_off_focused_holo_light.png 418B
reversal_up.png 1KB
run.gif 359KB
icon_news_back.png 1KB
bmob_update_btn_check_on_pressed_holo_light.png 2KB
bmob_update_btn_check_on_focused_holo_light.png 1KB
bmob_update_close_bg_normal.png 3KB
bmob_update_btn_check_off_holo_light.png 242B
bmob_update_close_bg_tap.png 2KB
AndroidManifest.xml 3KB
proguard-rules.pro 1KB
build.gradle 3KB
.gitignore 7B
.idea
checkstyle-idea.xml 254B
README.md 20KB
共 95 条
- 1
资源评论
妄北y
- 粉丝: 1w+
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功