首先看最重要的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. chorme requestBody

    https://stackoverflow.com/questions/18534771/chrome-extension-how-to-get-http-response-body Chrome w ...

  2. QML与C++交互:登陆界面设计

    QML与C++交互:登陆界面设计 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:WIN7 开发环境:Qt5.2.1 说明: QML设计前 ...

  3. MySQL-删除数据(DELECT)

    数据库备份介绍: 数据库一旦删除数据,它就会永远消失. 因此,在执行DELETE语句之前,应该先备份数据库,以防万一要找回删除过的数据. MySQL提供了非常有用的工具,用于在服务器上本地备份或转储M ...

  4. SQLServer2012连接数据库报错

    尝试读取或写入受保护的内存 这通常指示其他内... CMD 输入 netsh winsock reset,重启计算机即可

  5. 【bzoj4604】The kth maximum number

    暴力 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> ...

  6. linux内核对块设备的使用

    1 partition table 这里的分析以经典的MBR为例. 在MBR里面有partition table,每一项对应一个逻辑的块设备,partion table中的每一项是16个字节. 第一个 ...

  7. vijos - P1077克隆龙 (找规律 + 指数型母函数 + python)

    P1077克隆龙 Accepted 标签:[显示标签] 描写叙述 如今龙的克隆已成为可能,龙基因由ACTG字母组成,而龙的基因有例如以下特点: 1.A在基因中的出现为偶数次(包含0): 2.C的情况也 ...

  8. go6---slice切片

    package main /* 切片Slice 其本身并不是数组,它指向底层的数组 作为变长数组的替代方案,可以关联底层数组的局部或全部 为引用类型 可以直接创建或从底层数组获取生成 使用len()获 ...

  9. 【Silverlight】Bing Maps学习系列(一):开发前的准备工作

    [Silverlight]Bing Maps学习系列(一):开发前的准备工作 微软推出的Bing Maps地图引擎,对外开放了Silverlight和Ajax两种客户端API,同时微软针对全球地图还推 ...

  10. [Codeforces 639B] Bear and Forgotten Tree 3

    [题目链接] https://codeforces.com/problemset/problem/639/B [算法] 当d > n - 1或h > n - 1时 , 无解 当2h < ...