安卓中选择系统图库的图片及调用相机的源码(原创)
来源:互联网 发布:can软件 编辑:程序博客网 时间:2024/05/21 08:44
在Android开发中经常会用到图片的上传功能,一般常用的是调用Android系统自带的图库及拍照功能。示例代码如下:
在MainActivity.java中
import java.io.ByteArrayOutputStream;import java.io.File;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity {private Button btn1,btn2;private ImageView img;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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);img = (ImageView) findViewById(R.id.avatar);btn1 = (Button) findViewById(R.id.selector);btn2 = (Button) findViewById(R.id.paizhao);btn1.setOnClickListener(new OnClickListener() {@Overridepublic 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);}});btn2.setOnClickListener(new OnClickListener() {@Overridepublic 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); }@Overrideprotected 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); img.setImageBitmap(photo); } } }super.onActivityResult(requestCode, resultCode, data);}}activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fcf3ef" android:orientation="vertical" android:gravity="center_horizontal" android:padding="30dp" ><ImageView android:id="@+id/avatar" android:layout_width="130dp" android:layout_height="130dp" android:background="@drawable/ic_launcher" /><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/selector" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="1" android:gravity="center" android:text="本地选择"/> <Button android:id="@+id/paizhao" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="1" android:text="拍照"/></LinearLayout> </LinearLayout>效果如图:
因为时间关系,界面就谁便做了,请见谅!!!!!!!!!!!!!!!!!!!!
2 0
- 安卓中选择系统图库的图片及调用相机的源码(原创)
- 调用系统相机、图库并选择图片
- android开发中调用系统的相机和图库及图片的后期处理
- Android调用系统的相机,图库,以及裁剪图片功能。
- 调用系统相机和图库册得到图片的例子
- 调用系统相机,截图工具与图库(调用系统的图片内容提供者)
- 调用系统相机和图库,裁剪图片
- android 调用图库中选择的图片
- Android 调用系统相机拍照(适配7.0)、从图库选择图片;从数据库读取联系人信息、相册图片
- 弹出对话框选择相机或者图库内的图片
- 调用系统的相机 保存图片指定路径,供图库查看
- Android调用相机拍照并返回路径和调用系统图库选择图片
- Android调用相机拍照并返回路径和调用系统图库选择图片
- android调用系统相机,系统图库,系统剪裁图片
- iOS 调用相机拍照和选择图库图片 设置头像
- 调用Android中相机或图库选择图片并剪切
- Android启动系统相机,打开图库选择图片
- Intent选择器,开系统图库选择图片,开相机拍照
- 设计思绪
- NSString 截取字符串的某一个部分
- 安卓NavigationDrawer的使用
- NYOJ 119 士兵杀敌(三)【RMQ】
- js闭包学习
- 安卓中选择系统图库的图片及调用相机的源码(原创)
- 【设计模式】职责链模式
- SpringMVC的各种参数绑定方式
- cocos2dx常见32种场景切换动画
- JSF和Servlet的区别
- 文章标题
- FastDfs分布式文件系统+Ngnix安装
- Java注解和反射机制(1)
- spring jdbc多条件查询(参数化传参,防止sql注入风险)