Android中拍照和从相册选择图片,并对选择的图片进行剪裁的例子
2016-05-20 15:22
471 查看
效果如下:
1、[b]布局文件activity_main.xml 和 dialog.xml[/b]
<span style="font-size:14px;"><RelativeLayout 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"
tools:context=".MainActivity" >
<Button
android:id="@+id/photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="相机"
android:gravity="center"
/>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/photo"
android:background="@drawable/ic_launcher"/>
</RelativeLayout></span>
[title3] 2. dialog.xml[/title3]
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/shoot"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="20dp"
android:textSize="20sp"
android:background="#808080"
android:gravity="center"
android:text="拍摄图片"/>
<TextView
android:id="@+id/album"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:textSize="20sp"
android:background="#808080"
android:gravity="center"
android:text="相册图片"/>
</LinearLayout></span>
[title3]3. MainActivity.java[/title3]
<span style="font-size:14px;">package com.zhang.photoimage;
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.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView img;
private Button btn;
private View myDialog;
private AlertDialog alertDialog;
private View dialogView;
private TextView shoot,album;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button) findViewById(R.id.photo);
img=(ImageView) findViewById(R.id.image);
showDialog();
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.show();//显示对话框
}
});
}
private void showDialog(){
dialogView=LayoutInflater.from(this).
inflate(R.layout.dialog, null);
shoot=(TextView) dialogView.findViewById(R.id.shoot);
album=(TextView) dialogView.findViewById(R.id.album);
//拍摄图片
shoot.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
getImageFromCamera();
}
});
//从相册选择图片
album.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//关闭对话框
alertDialog.dismiss();
//从相册获取照片
getImageFromAlbum();
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(this);
alertDialog = builder.setView(dialogView).create();
}
/**
* 拍照
*/
protected void getImageFromCamera() {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment
.getExternalStorageDirectory(),"temp.jpg")));
startActivityForResult(intent, 1);
} else {
Toast.makeText(this, "请确认已经插入SD卡", Toast.LENGTH_LONG).show();
}
}
/**
* 从相册获取照片
*/
protected void getImageFromAlbum() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");// 相片类型
startActivityForResult(intent, 2);
}
/**
* 收缩图片
*
* @param uri
*/
public void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 500);
intent.putExtra("outputY", 500);
intent.putExtra("return-data", true);
startActivityForResult(intent, 3);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == 0)
return;
// 拍照
if (requestCode == 1) {
// 设置文件保存路径
File picture = new File(Environment.getExternalStorageDirectory()
+ "/temp.jpg");
startPhotoZoom(Uri.fromFile(picture));
}
if (data == null)
return;
// 读取相册缩放图片
if (requestCode == 2) {
startPhotoZoom(data.getData());
}
// 处理结果
if (requestCode == 3) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0-100)压缩文件
//此处可以把Bitmap保存到sd卡中,具体请看:http://www.cnblogs.com/linjiqin/archive/2011/12/28/2304940.html
img.setImageBitmap(photo); //把图片显示在ImageView控件上
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}</span><span style="font-size:18px;">
</span>
相关文章推荐
- 手把手教你安卓入门(一)
- Android动画原理分析
- android:hintText与android:inputType详解
- Android中Activity启动过程探究
- Android中visibility属性VISIBLE、INVISIBLE、GONE的区别
- Activity的管理模型,C/S交互
- android N
- Android内存泄露自动检测LeakCanary
- 下载及编译Android6.0异常解决
- android集成百度地图SDK 自定义keystore无法编译 Keystore was tampered with, or password was incorrect的解决方法
- Android自定义View——Paint之Xfermode
- 简单实现Android NDK编译jni调用动态库开发
- 深入理解android6.0 RunTime Permisstion
- Android强制设置横屏或竖屏
- 深入理解android6.0 RunTime Permisstion
- 关于Android5.0+去掉标题栏的研究
- android 混淆jar 或者library混淆
- 关于android studio报错transformClassesAndResourcesWithProguardForRelease
- [Android 小应用]batterymonitor 电源监控
- android app与h5交互