图库和拍照功能
来源:互联网 发布:vivo手机销售数据 编辑:程序博客网 时间:2024/05/16 08:55
转别人的,自己学习下
java代码
package com.example.test;import java.io.ByteArrayOutputStream;import java.io.File;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.ImageView;import android.widget.LinearLayout;public class MainActivity extends Activity { public final static int PHOTO_ZOOM = 0; public final static int TAKE_PHOTO = 1; public final static int PHOTO_RESULT = 2; public static final String IMAGE_UNSPECIFIED = "image/*"; private String imageDir;private ImageView avatar; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); avatar = (ImageView) findViewById(R.id.avatar); // 本地图库选择按钮 LinearLayout upload = (LinearLayout)findViewById(R.id.select_button); upload.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType(IMAGE_UNSPECIFIED); Intent wrapperIntent=Intent.createChooser(intent, null); startActivityForResult(wrapperIntent, PHOTO_ZOOM); } }); // 拍照按钮 LinearLayout takePhoto=(LinearLayout)findViewById(R.id.photo_button); takePhoto.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { imageDir = "temp.jpg"; Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), imageDir))); startActivityForResult(intent, TAKE_PHOTO); } });} // 图片缩放 public void photoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, IMAGE_UNSPECIFIED); intent.putExtra("crop", "true"); // aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY 是裁剪图片宽高 intent.putExtra("outputX", 250); intent.putExtra("outputY", 250); intent.putExtra("return-data", true); startActivityForResult(intent, PHOTO_RESULT); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { if (requestCode == PHOTO_ZOOM) { photoZoom(data.getData()); } if (requestCode == TAKE_PHOTO) { File picture = new File(Environment.getExternalStorageDirectory() + "/" + imageDir); photoZoom(Uri.fromFile(picture)); } if (requestCode == PHOTO_RESULT) { Bundle extras = data.getExtras(); if (extras != null) { Bitmap photo = extras.getParcelable("data"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.JPEG, 75, stream); avatar.setImageBitmap(photo); } } } super.onActivityResult(requestCode, resultCode, data); }}
xml代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#fcf3ef" android:gravity="center_horizontal" android:orientation="vertical" android:paddingTop="30dp" > <ImageView android:id="@+id/avatar" android:layout_width="130dp" android:layout_height="130dp" android:background="@drawable/avatar_frame" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:gravity="center_horizontal" ><!-- 图库 --> <LinearLayout android:id="@+id/select_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <ImageView android:layout_width="54dp" android:layout_height="54dp" android:background="@drawable/local" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="本地选择" android:textColor="#000000" android:textSize="20sp" /> </LinearLayout><!-- 拍照 --> <LinearLayout android:id="@+id/photo_button" android:layout_width="126dp" android:layout_height="match_parent" android:layout_marginLeft="50dp" android:gravity="center" android:orientation="vertical" > <ImageView android:layout_width="57dp" android:layout_height="53dp" android:background="@drawable/take_photo" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="拍照" android:textColor="#000000" android:textSize="20sp" /> </LinearLayout> </LinearLayout></LinearLayout>
0 0
- 图库和拍照功能
- 图库和拍照
- Android 拍照和图库功能(适配Android 6.0和7.0系统和华为机型问题)
- Andorid调用系统的拍照功能和系统图库对图片进行裁剪
- 好用的调用图库和拍照并裁剪功能(适用于上传头像)
- Ionic调用摄像头拍照和选择图库照片功能的注意点
- 利用系统相机和图库打造3分钟完成拍照,图片裁剪以及视频资源上传功能
- 上传文件之获取拍照照片和本地图库
- Android 拍照获取图片和图库获取图片
- 从本地图库获取图片和拍照获取图片
- iOS 调用相机拍照和选择图库图片 设置头像
- Android 4.4前后版本读取图库图片和拍照方法
- Android 4.4前后版本读取图库图片和拍照
- appcan拍照上传和图库选择上传改进版
- Android 拍照和选择图库的demo(兼容7.0)
- 相机拍照与图库
- android 录像和拍照功能
- android 录像和拍照功能
- 12-面试题总结2015年3月9日第二发
- java protected 与默认权限的区别
- (4.2.12.4)【android开源组件】极光推送JPush
- HTTP Status 500 - Could not open JDBC Connection for transaction; nested exception is java.lang.NoCl
- 求链表的中间节点
- 图库和拍照功能
- .NET MVC处理POST跨越请求方法
- 一致性哈希算法及其在分布式系统中的应用
- Java内存模型与垃圾收集
- eclipse安装gradle插件步骤和使用
- 死锁
- Quick-cocos2d-x的MVC架构之controller研究
- (4.2.13)【android开源工具】科大讯飞开放平台
- CDN内容分发网络技术原理