Android显示相册图片和相机拍照
首先看最重要的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显示相册图片和相机拍照的更多相关文章
- Android显示GIF图片
今天我们研究一下怎样在Android手机上显示GIF动态图片 首先须要在src文件夹下新建一个自己定义的View.代码例如以下: </pre><pre name="code ...
- android 开发 实现一个进入相机拍照后裁剪图片或者进入相册选中裁剪图片的功能
实现思维路径: 以进入相机拍照的思维路线为例子: 1.进入app 2.判断之前是否保存头像,如果有就显示历史图像 (下面代码中在getOldAvatar();方法中执行这个逻辑) 3.点击更换图像的B ...
- [转]微信小程序开发之从相册获取图片 使用相机拍照 本地图片上传
本文转自:http://blog.csdn.net/qq_31383345/article/details/53014610 今天遇到微信小程序的用户头像设置功能,做笔记. 先上gif: 再上代码: ...
- 微信小程序开发之从相册获取图片 使用相机拍照 本地图片上传
1.index.wxml <!--index.wxml--> <button style="margin:30rpx;" bindtap="choose ...
- Xamarin.Android 入门之:Bind java的jar文件+Android显示gif图片
一.引言 在xamarin开发的时候,有时我们想要做一个功能,但是这个功能已经有人用java写好了,并且打包成了jar文件.那么我们可以直接把对方的jar文件拿过来用而不是重新用c#写代码. 关于bi ...
- Android获取相册图片
1. AlertDialog的使用 2. 显示和隐式意图的区别 3. 相册页面的跳转 4. 选择完成后返回图片的获取 ----------------------------------------- ...
- Android笔记之调用系统相机拍照
参考链接: 拍照 | Android Developers, Android相机拍照方向旋转的解决方案:ExifInterface - 简书 Demo链接:https://pan.baidu.co ...
- android 显示gif图片
在android中不支持gif格式的图片,但是由于我希望在我的程序中刚刚加载的时候有一个小人在跑步表示正在加载.而这个小人跑就是一个gif图片.也就是希望程序一启动时就加载gif图片.在网上查找了一些 ...
- 新浪微博客户端(31)-显示相册图片上的GIF标记
DJStatusPhotoView.h #import <UIKit/UIKit.h> @class DJPhoto; @interface DJStatusPhotoView : UII ...
随机推荐
- vue assetsPublicPath
vue 中 /config/index.js, assetsPublicPath 的作用是便于访问打包后的静态资源,默认是相对于根 /, 当然如果直接把dist文件夹当成根来配置域名 可以什么都不用 ...
- 条款二:最好使用c++转型操作符
一.static_cast基本上拥有与c旧式转型相同的威力与意义,以及相同的限制,不能够移除表达式的常量性,const_cast负责这个功能 二.const_cast用来改变表达式中的常量性,如果将c ...
- JS原生DOM操作总结
DOM的主要操作——增.删.改.查节点 (1) 查找节点 document.getElementById('div1') document.getElementsByName('uname') doc ...
- CodeIgniter 向mysql插入数据包括字母、汉字问题
今天在使用ci框架,须要向mysql数据表插入数据.当中的一个字段包括汉字.字母.但是用传统的使用sql语句:insert into XXX这样的方式,不管怎样都插入不成功,最后我换了还有一种方式: ...
- 慎用Outline ,UGUI Outline实现原理分析
使用 UGUI 制作背包的时候.同事发现假设背包中加入了大量的物品.比方两百个.Unity就会出错,提示 Canvas element contains more than 65535 vertice ...
- Android网络爬虫程序(基于Jsoup)
摘要:基于 Jsoup 实现一个 Android 的网络爬虫程序,抓取网页的内容并显示出来.写这个程序的主要目的是抓取海投网的宣讲会信息(公司.时间.地点)并在移动端显示,这样就可以随时随地的浏览在学 ...
- hdu 1009 FatMouse' Trade
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Oracle 远程访问配置 在 Windows Forms 和 WPF 应用中使用 FontAwesome 图标 C#反序列化XML异常:在 XML文档(0, 0)中有一个错误“缺少根元素” C#[Win32&WinCE&WM]应用程序只能运行一个实例:MutexHelper Decimal类型截取保留N位小数向上取, Decimal类型截取保留N位小数并且不进行四舍五入操作
Oracle 远程访问配置 服务端配置 如果不想自己写,可以通过 Net Manager 来配置. 以下配置文件中的 localhost 改为 ip 地址,否则,远程不能访问. 1.网络监听配置 ...
- linux服务器上的mysql允许远程连接
首先进入mysql: 输入GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; 这里是 ...
- 通过spark rdd 求取 特征的稀疏向量
通过spark rdd 求取 特征的稀疏向量 spark 类标签的稀疏 特征向量 - bonelee - 博客园 http://www.cnblogs.com/bonelee/p/7814081.h ...