Android 6.0及以上版本如何实现从图库中选取图片和拍照功能
XML 代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
> <Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="takePhone"
android:text="拍照"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="choosePhone"
android:text="相册选择"
/> <ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
JAVA 代码:
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private static final int CROP_PHOTO = 2;
private static final int REQUEST_CODE_PICK_IMAGE=3;
private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 6;
private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE2 = 7;
private File output;
private Uri imageUri; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initView();
} void initView(){ imageView=(ImageView)findViewById(R.id.image);
} public void takePhone(View view){ if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_CALL_PHONE2); }else { takePhoto();
}
} public void choosePhone(View view){ if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_CALL_PHONE2); }else { choosePhoto();
}
} /**
* 拍照
*/
void takePhoto(){
/**
* 最后一个参数是文件夹的名称,可以随便起
*/
File file=new File(Environment.getExternalStorageDirectory(),"拍照"); if(!file.exists()){ file.mkdir();
}
/**
* 这里将时间作为不同照片的名称
*/
output=new File(file,System.currentTimeMillis()+".jpg"); /**
* 如果该文件夹已经存在,则删除它,否则创建一个
*/
try {
if (output.exists()) { output.delete();
}
output.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
/**
* 隐式打开拍照的Activity,并且传入CROP_PHOTO常量作为拍照结束后回调的标志
*/
imageUri = Uri.fromFile(output);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CROP_PHOTO); } /**
* 从相册选取图片
*/
void choosePhoto(){
/**
* 打开选择图片的界面
*/
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");//相片类型
startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE); } public void onActivityResult(int req, int res, Intent data) {
switch (req) {
/**
* 拍照的请求标志
*/
case CROP_PHOTO:
if (res==RESULT_OK) {
try {
/**
* 该uri就是照片文件夹对应的uri
*/
Bitmap bit = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
imageView.setImageBitmap(bit); } catch (Exception e) { Toast.makeText(this,"程序崩溃",Toast.LENGTH_SHORT).show();
}
}
else{ } break;
/**
* 从相册中选取图片的请求标志
*/
case REQUEST_CODE_PICK_IMAGE:
if (res == RESULT_OK) {
try {
/**
* 该uri是上一个Activity返回的
*/
Uri uri = data.getData();
Bitmap bit = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
imageView.setImageBitmap(bit);
} catch (Exception e) { e.printStackTrace();
Toast.makeText(this,"程序崩溃",Toast.LENGTH_SHORT).show();
}
}
else{ }
break; default:
break;
}
} @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{ if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE)
{
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
takePhoto(); } else
{
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
} if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE2)
{
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
choosePhoto(); } else
{
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
Android 6.0及以上版本如何实现从图库中选取图片和拍照功能的更多相关文章
- Android Camera开发系列(上)——Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片
Android Camera开发系列(上)--Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片 最近也是在搞个破相机,兼容性那叫一个不忍直视啊,于是自己翻阅了一些基本的资料,自己实现了一 ...
- Android 6.0 M userdebug版本执行adb remount失败
[FAQ18076]Android 6.0 M版本默认会打开system verified boot,即在userdebug和user版本会把system映射到dm-0设备,然后再挂载.挂载前会检查s ...
- Android 5.0及以上版本使用webview不能存储第三方Cookies解决方案
Android 5.0以上的手机使用原生WebView浏览网页,在进行登录的时候会提示验证码错误,通过查找5.0以上系统的api文档,发现5.0以上版本的webview做了较大的改动,如:同步cook ...
- https Android 5.0 以下TLS 版本过低造成的问题
异常如下 javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ...
- Android 4.0及以上版本接收开机广播BOOT_COMPLETED、开机自启动服务
1.BootCompletedReceiver.Java文件 public class BootCompletedReceiver extends BroadcastReceiver { @Overr ...
- tomcat https 支持android 6.0及以上版本的配置方法
<Connector port="443" protocol="HTTP/1.1" SSLEnabled="true" scheme ...
- 浅谈Android 6.0之Runtime Permissions
前言 Android6.0发布后,其一系列新特新足够让我们这些Android程序员兴奋一段时间了.首先我们先看看具体有哪些新特性: -锁频下语音搜索 -指纹识别 -更完整的应用权限管理 -Doze电量 ...
- JRebel for Android 1.0发布!
什么是JRebel for Android? 一款Android studio插件——允许你修改正在运行中的应用程序,而且不必重新部署或重启.支持所有运行Android 4.0及以上版本的手机和平板. ...
- Android 6.0 双卡拨号
相关 api getCallCapablePhoneAccountsAdded in API level 23 Android 5.0 之前的版本 Call from second sim 获取 si ...
随机推荐
- vs2017打包exe安装包
1,安装扩展程序Install Projects 2,在打开的界面搜索Install,找到Install Projects 3,在要打包的项目解决方案下创建一个生成exe的项目 4,在打包项目的文件系 ...
- Redis 服务端程序实现原理
上篇我们简单介绍了 redis 客户端的一些基本概念,包括其 client 数据结构中对应的相关字段的含义,本篇我们结合这些,来分析分析 redis 服务端程序是如何运行的.一条命令请求的完成,客户端 ...
- 返回运行方法,可以写在一行 callback&&callback()
return DiscountMap[discountType] && DiscountMap[discountType](price)
- LSTM + linear-CRF序列标注笔记
CRF 许多随机变量组成一个无向图G = {V, E},V代表顶点,E代表顶点间相连的边, 每个顶点代表一个随机变量,边代表两个随机变量间存在相互影响关系(变量非独立), 如果随机变量根据图的结构而具 ...
- appium移动自动化详解
1移动自动化简介 移动自动化就是通过代码来控制手机,模拟人的动作,对手机进行一些点击,输入等操作,那python代码如何能控制到手机呢?目前的思路应该是python代码->Appium-pyth ...
- Nacos作为微服务注册中心,爱不释手的感觉
我觉得Nacos用起来还不错 在使用SpringCloud做分布式微服务架构时,注册中心是必不可少的一个组件.目前可以用的主要有:Eureka.Consul.Zookeeper.今天,我们就来说一下A ...
- 从零开始学习R语言(八)——R语言绘图
本文首发于知乎专栏:https://zhuanlan.zhihu.com/p/74051739 也同步更新于我的个人博客:https://www.cnblogs.com/nickwu/p/125683 ...
- python之路---协程
阅读目录 一 引子 二 协程介绍 三 Greenlet模块 四 Gevent模块 引子 之前我们学习了线程.进程的概念,了解了在操作系统中进程是资源分配的最小单位,线程是CPU调度的最小单位.按道理来 ...
- Anaconda常用命令收藏----后期还会更新
一.更换jupyter notobook的打开目录 一般载在安装好Anaconda的时候,打开jupyter指向的目录一般是系统的根目录,如C:\Users\25282,但是这样的话对C盘是个不小的负 ...
- OpenCV-Python 图像上的算术运算 | 十一
目标 学习图像的几种算术运算,例如加法,减法,按位运算等. 您将学习以下功能:cv.add,cv.addWeighted等. 图像加法 您可以通过OpenCV函数cv.add()或仅通过numpy操作 ...