首先看最重要的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. golang 查询数据库操作

    SQL.Open only creates the DB object, but dies not open any connections to the database. If you want ...

  2. 【Akka】Actor模型探索

    Akka是什么 Akka就是为了改变编写高容错性和强可扩展性的并发程序而生的.通过使用Actor模型我们提升了抽象级别,为构建正确的可扩展并发应用提供了一个更好的平台.在容错性方面我们採取了" ...

  3. Mariadb 索引及外键

    索引 索引相当于一本书的目录,在一个数据库或表有索引的情况下,会很便于查询数据,使查询更加效率,相对的也有缺点,不利于去修改,比较麻烦,有索引便于查询,那就意味着索引创建的越多越好么?然而并不是:索引 ...

  4. 【bzoj1406】[AHOI2007]密码箱

    x2 ≡ 1 mod n => x2 = k * n + 1 => n | (x + 1) * (x - 1) 令n = a * b,则 (a | x + 1 且 b | x - 1) 或 ...

  5. luogu2827 蚯蚓

    题目大意 本题中,我们将用符号[c]表示对c向下取整,例如:[3.0」= [3.1」= [3.9」=3. 蛐蛐国最近蚯蚓成灾了!隔壁跳蚤国的跳蚤也拿蚯蚓们没办法,蛐蛐国王只好去请神刀手来帮他们消灭蚯蚓 ...

  6. Nginx入门详解文档

    1 文章内容 掌握nginx+tomcat反向代理的使用方法. 掌握nginx作为负载均衡器的使用方法. 掌握nginx实现web缓存方法. 2 nginx介绍 2.1 什么是nginx Nginx是 ...

  7. ZOJ 1871:Steps

    Steps Time Limit: 2 Seconds      Memory Limit: 65536 KB One steps through integer points of the stra ...

  8. PL/SQL -->隐式游标(SQL%FOUND)

    PL/SQL -->隐式游标(SQL%FOUND) 分类: SQL/PLSQL 基础2010-12-22 16:23 4084人阅读 评论(0) 收藏 举报 sqlexceptionoracle ...

  9. luence全文检索(数据库检索)

    注解:从数据库中查询所有数据然后放入luence中,然后在luence来检索 package com.zhu.demo; import java.io.IOException; import java ...

  10. POJ3675 Telescope 圆和多边形的交

    POJ3675 用三角剖分可以轻松搞定,数据也小 随便AC. #include<iostream> #include<stdio.h> #include<stdlib.h ...