Android开发之调用系统图库及相机
2013-09-22 17:19
441 查看
Android开发之调用系统图库及相机
在Android开发中经常会用到图片的上传功能,一般常用的是调用Android系统自带的图库及拍照功能。示例代码如下:
Java代码:
布局文件:
布局文件效果图如下:
本地选择效果图如下:
相机拍照效果如下:
图片缩放效果图:
图片上传结果效果图:
在Android开发中经常会用到图片的上传功能,一般常用的是调用Android系统自带的图库及拍照功能。示例代码如下:
Java代码:
public class TestActivity 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.main); avatar = (ImageView) findViewById(R.id.avatar); // 本地图库选择按钮 LinearLayout upload = (LinearLayout)findViewById(R.id.local_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.take_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 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/local_select_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" 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/take_photo_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:gravity="center" android:orientation="vertical" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" 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> |
本地选择效果图如下:
相机拍照效果如下:
图片缩放效果图:
图片上传结果效果图:
相关文章推荐
- Android开发之调用系统图库及相机
- android开发中调用系统的相机和图库及图片的后期处理
- Android 调用系统相机、图库 ,取图片并裁剪、保存sd卡
- (复习)android 调用系统相机 图库 裁剪-图片上传-客服端-服务器
- Android调用系统相机和图库设置到控件上并上传服务器
- android 调用系统相机和图库 实例详解
- Android 调用系统相机拍照(适配7.0)、从图库选择图片、从数据库读取联系人信息、相册图片、扫描文件
- Android开发学习之调用系统相机完成拍照的实现
- Android开发之调用相机拍照和调用系统相册
- Android调用相机拍照并返回路径和调用系统图库选择图片
- Android:调用系统相机 图库 裁剪-图片上传-客服端-服务器
- Android调用系统相机与图库
- Android中调用系统相机,图库以及裁切遇到的坑
- Android实现调用系统图库与相机设置头像并保存在本地及服务器
- Android开发之调用相机拍照与本地图库选择图片
- android 系统4.4调用本地图库和系统相机
- Android开发调用系统的图库
- Android开发片段–调用系统相机拍照
- Android调用相机拍照并返回路径和调用系统图库选择图片
- android调用系统相机,系统图库,系统剪裁图片