首先看最重要的MainActive类:

public class MainActivity extends AppCompatActivity {

    private final int FROM_ALBUM = 1;//表示从相册获取照片
private final int FROM_CAMERA = 2;//表示从相机获取照片
private ImageView imageView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textView = (TextView)findViewById(R.id.textView);
setContentView(R.layout.activity_main); applyWritePermission();//请求权限 } // 打开相册
public void onClickAlbum(View view){
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, FROM_ALBUM);
} // 打开相机
public void onClickCamera(View view){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, FROM_CAMERA);
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){ //从相册返回
if(requestCode == FROM_ALBUM && resultCode == Activity.RESULT_OK && data != null){
imageView = (ImageView)findViewById(R.id.imageView);
textView = (TextView)findViewById(R.id.textView);
Uri imageUri = data.getData();
ContentResolver cr = this.getContentResolver();
try {
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(imageUri)); int res = FaceClassified.runClassified(bitmap); imageView.setImageBitmap(bitmap);
}catch (FileNotFoundException e){
Log.e("Exception", e.getMessage(), e);
}
} //从相机返回
if(requestCode == FROM_CAMERA && resultCode == Activity.RESULT_OK && data != null){
imageView = (ImageView)findViewById(R.id.imageView);
textView = (TextView)findViewById(R.id.textView); Bitmap photo = (Bitmap) data.getExtras().get("data"); int res = FaceClassified.runClassified(photo); imageView.setImageBitmap(photo);
} super.onActivityResult(requestCode, resultCode, data);
} private void applyWritePermission() { String permissions1 = Manifest.permission.WRITE_EXTERNAL_STORAGE;
String permissions2 = Manifest.permission.READ_EXTERNAL_STORAGE;
String permissions3 = Manifest.permission.CAMERA; if (Build.VERSION.SDK_INT >= 23) {
int check1 = ContextCompat.checkSelfPermission(this, permissions1);
if (check1 != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
int check2 = ContextCompat.checkSelfPermission(this, permissions2);
if (check2 != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
int check3 = ContextCompat.checkSelfPermission(this, permissions3);
if (check3 != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, 1);
}
}
}
}

上面两个按钮的处理函数名称在布局中定义,布局如下:两个button(一个打开相册,一个打开相机),一个imageview

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.meitu.graydemo.MainActivity"> <LinearLayout
android:layout_width="368dp"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="8dp"> <LinearLayout
android:id="@+id/buttonLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickAlbum"
android:text="打开相册"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="16dp" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickCamera"
android:text="打开相机"
tools:layout_editor_absoluteX="280dp"
tools:layout_editor_absoluteY="16dp" />
</LinearLayout> <ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@android:color/holo_blue_bright"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="48dp" /> </LinearLayout> </android.support.constraint.ConstraintLayout>

Android显示相册图片和相机拍照的更多相关文章

  1. Android显示GIF图片

    今天我们研究一下怎样在Android手机上显示GIF动态图片 首先须要在src文件夹下新建一个自己定义的View.代码例如以下: </pre><pre name="code ...

  2. android 开发 实现一个进入相机拍照后裁剪图片或者进入相册选中裁剪图片的功能

    实现思维路径: 以进入相机拍照的思维路线为例子: 1.进入app 2.判断之前是否保存头像,如果有就显示历史图像 (下面代码中在getOldAvatar();方法中执行这个逻辑) 3.点击更换图像的B ...

  3. [转]微信小程序开发之从相册获取图片 使用相机拍照 本地图片上传

    本文转自:http://blog.csdn.net/qq_31383345/article/details/53014610 今天遇到微信小程序的用户头像设置功能,做笔记. 先上gif: 再上代码: ...

  4. 微信小程序开发之从相册获取图片 使用相机拍照 本地图片上传

    1.index.wxml <!--index.wxml--> <button style="margin:30rpx;" bindtap="choose ...

  5. Xamarin.Android 入门之:Bind java的jar文件+Android显示gif图片

    一.引言 在xamarin开发的时候,有时我们想要做一个功能,但是这个功能已经有人用java写好了,并且打包成了jar文件.那么我们可以直接把对方的jar文件拿过来用而不是重新用c#写代码. 关于bi ...

  6. Android获取相册图片

    1. AlertDialog的使用 2. 显示和隐式意图的区别 3. 相册页面的跳转 4. 选择完成后返回图片的获取 ----------------------------------------- ...

  7. Android笔记之调用系统相机拍照

    参考链接: 拍照  |  Android Developers, Android相机拍照方向旋转的解决方案:ExifInterface - 简书 Demo链接:https://pan.baidu.co ...

  8. android 显示gif图片

    在android中不支持gif格式的图片,但是由于我希望在我的程序中刚刚加载的时候有一个小人在跑步表示正在加载.而这个小人跑就是一个gif图片.也就是希望程序一启动时就加载gif图片.在网上查找了一些 ...

  9. 新浪微博客户端(31)-显示相册图片上的GIF标记

    DJStatusPhotoView.h #import <UIKit/UIKit.h> @class DJPhoto; @interface DJStatusPhotoView : UII ...

随机推荐

  1. centos7更新、更新、每天更新、每天自动更新

    每一天我们的系统时时刻刻都被凶狠之徒盯着,保持软件在最新的状态是其中一项我们必须做,也很容易做到的工作. 首先我们立即手动更新所有预先安装的软件: yum -y update 跟着设定系统定时自动更新 ...

  2. python可变參数调用函数问题

    一直使用python实现一些想法,近期在使用python的过程中出现这样一个需求,定义了一个函数.第一个是普通參数.第二个是默认參数,后面还有可变參数,在最初学习python的时候,都知道非keywo ...

  3. 『GCD』详解

    2. GCD 任务和队列 学习 GCD 之前,先来了解 GCD 中两个核心概念:任务和队列. 任务:就是执行操作的意思,换句话说就是你在线程中执行的那段代码.在 GCD 中是放在 block 中的.执 ...

  4. 工作总结 2018-4-13 bootstrapTable 属性 queryParams: queryParams,//参数 get 中 %5B%5D 数组的意思

    <table id="dataTable" data-toggle="table" data-show-columns="true" ...

  5. Unity3D研究之多语言用中文做KEY

     做多语言的时候用中文做KEY绝对是有100%的优点,假设用英文表示那么代码里面给文字赋值的地方全都是英文.写的代码多了以后维护起来就没有人能看懂了,或者看起来非常费劲. 对PoolManager ...

  6. Android 代码设置Activity 背景透明

    当指定Activity 样式 Them.Dialog 时候 又不同意用XML 设置 Activity 的背景颜色的时候 用代码 this.getWindow().getDecorView().setB ...

  7. 20160225.CCPP体系具体解释(0035天)

    程序片段(01):CircleList.h+CircleList.c+main.c 内容概要:环形链表 ///CircleList.h #pragma once #include <stdio. ...

  8. IE6\7\8 :last-child 和 :first-chlid 兼容

    IE9以下不支持last-child ,只支持first-child,边框尽量用上边框.

  9. SNAPSHOTTING

    /etc/redis.conf ################################ SNAPSHOTTING ################################## Sav ...

  10. oracle-扫盲贴:存储过程实现增删改查

    原文引入:http://blog.csdn.net/yangzhawen/article/details/8617179 oracle-扫盲贴:存储过程实现增删改查 分类: oracle2013-02 ...