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及以上版本如何实现从图库中选取图片和拍照功能的更多相关文章

  1. Android Camera开发系列(上)——Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片

    Android Camera开发系列(上)--Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片 最近也是在搞个破相机,兼容性那叫一个不忍直视啊,于是自己翻阅了一些基本的资料,自己实现了一 ...

  2. Android 6.0 M userdebug版本执行adb remount失败

    [FAQ18076]Android 6.0 M版本默认会打开system verified boot,即在userdebug和user版本会把system映射到dm-0设备,然后再挂载.挂载前会检查s ...

  3. Android 5.0及以上版本使用webview不能存储第三方Cookies解决方案

    Android 5.0以上的手机使用原生WebView浏览网页,在进行登录的时候会提示验证码错误,通过查找5.0以上系统的api文档,发现5.0以上版本的webview做了较大的改动,如:同步cook ...

  4. https Android 5.0 以下TLS 版本过低造成的问题

    异常如下 javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ...

  5. Android 4.0及以上版本接收开机广播BOOT_COMPLETED、开机自启动服务

    1.BootCompletedReceiver.Java文件 public class BootCompletedReceiver extends BroadcastReceiver { @Overr ...

  6. tomcat https 支持android 6.0及以上版本的配置方法

    <Connector port="443"  protocol="HTTP/1.1" SSLEnabled="true" scheme ...

  7. 浅谈Android 6.0之Runtime Permissions

    前言 Android6.0发布后,其一系列新特新足够让我们这些Android程序员兴奋一段时间了.首先我们先看看具体有哪些新特性: -锁频下语音搜索 -指纹识别 -更完整的应用权限管理 -Doze电量 ...

  8. JRebel for Android 1.0发布!

    什么是JRebel for Android? 一款Android studio插件——允许你修改正在运行中的应用程序,而且不必重新部署或重启.支持所有运行Android 4.0及以上版本的手机和平板. ...

  9. Android 6.0 双卡拨号

    相关 api getCallCapablePhoneAccountsAdded in API level 23 Android 5.0 之前的版本 Call from second sim 获取 si ...

随机推荐

  1. el-select下拉加载(实现懒加载)

    情况:项目出现了下拉数据量过大,出现页面卡死问题,反馈到我这:当时实现思路1.使用render函数去渲染下拉框 试了发现卡死情况依然存在,所以尝试方法2 2.使用原生js去添加下拉框的<opti ...

  2. MAC下安装Fiddler抓包工具

    需求 我们都知道在Mac电脑下面有一个非常好的抓包工具:Charles.但是这个只能抓代理的数据包.但是有时候想要调试本地网卡的数据库 Charles 就没办法了.就想到了在windows下面的一个F ...

  3. mysql那些事之索引篇

    mysql那些事之索引篇 上一篇博客已经简单从广的方面介绍了一下mysql整体架构以及物理结构的内容. 本篇博客的内容是mysql的索引,索引无论是在面试还是我们日常工作中都是非常的重要一环. 索引是 ...

  4. PDF顯示插件

    1. ie瀏覽器適用<object id="pdf_panel" class="pdf-panel" classid="clsid:CA8A97 ...

  5. 一起了解 .Net Foundation 项目 No.21

    .Net 基金会中包含有很多优秀的项目,今天就和笔者一起了解一下其中的一些优秀作品吧. 中文介绍 中文介绍内容翻译自英文介绍,主要采用意译.如与原文存在出入,请以原文为准. UWP Community ...

  6. 【简介】OpenOCD 由jtag操作到parport driver流程

    1. 定义 jtag_command_type 在 OpenOCD 中,JTag 命令在枚举 jtag_command_type 中定义,定义如下: /** * The type of the @c ...

  7. IdentityServer4源码解析_1_项目结构

    目录 IdentityServer4源码解析_1_项目结构 IdentityServer4源码解析_2_元数据接口 IdentityServer4源码解析_3_认证接口 IdentityServer4 ...

  8. Linux提权小结

    原文链接:http://zone.secevery.com/article/1104 Linux提权1.信息收集2.脏牛漏洞提权3.内核漏洞exp提权4.SUID提权 0x00 基础信息收集(1):内 ...

  9. JS三个事件绑定方法

    1. JS三个事件绑定方法 1.1. 使用html进行事件绑定 1.直接在html标签上写入事件类型和事件处理方法. <button onclick = "alert('hello w ...

  10. 通过源码理解Spring中@Scheduled的实现原理并且实现调度任务动态装载

    前提 最近的新项目和数据同步相关,有定时调度的需求.之前一直有使用过Quartz.XXL-Job.Easy Scheduler等调度框架,后来越发觉得这些框架太重量级了,于是想到了Spring内置的S ...