效果图:
/DEMO/res/layout/activity_main.xml
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="@+id/uploadPictureLayout" >
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="31dp"
android:layout_marginTop="21dp"
android:src="@drawable/add_image_icon" />
</RelativeLayout>
/DEMO/res/layout/alert_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/pop_layout"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:background="#000000"
android:gravity="center_horizontal"
android:orientation="vertical" >
<Button
android:id="@+id/btn_take_photo"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:layout_marginTop="15dip"
android:background="#3D3D3D"
android:text="现在拍照"
android:textSize="18sp"
android:textColor="#ffffff" />
<Button
android:id="@+id/btn_pick_photo"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:layout_marginTop="13dip"
android:background="#3D3D3D"
android:text="从图库中选择"
android:textSize="18sp"
android:textColor="#ffffff" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_marginBottom="15dip"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:layout_marginTop="13dip"
android:background="#3D3D3D"
android:text="取消"
android:textSize="18sp"
android:textColor="#ffffff" />
</LinearLayout>
</RelativeLayout>
com.example.demo.MainActivity
package com.example.demo;
import java.io.File;
import java.io.IOException;
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.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView imageView;
private SelectPicPopupWindow menuWindow;
private File file0;//图片路径,可上传到网络
private String path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(ImageView)findViewById(R.id.img);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
menuWindow=new SelectPicPopupWindow(MainActivity.this, itemsOnClick);
menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.uploadPictureLayout), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); // 设置layout在PopupWindow中显示的位置
}
});
}
private OnClickListener itemsOnClick=new OnClickListener() {
@Override
public void onClick(View v) {
menuWindow.dismiss();
switch (v.getId()) {
case R.id.btn_take_photo: {
// 拍照
takePhoto();
break;
}
case R.id.btn_pick_photo: {
// 打开选择图片界面
pickPhoto();
break;
}
default:
break;
}
}
};
/**
* 拍照获取图片
*/
private void takePhoto() {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
String saveDir = Environment.getExternalStorageDirectory() + "/temple";
File dir = new File(saveDir);
if (!dir.exists()) {
dir.mkdir();
}
file0 = new File(saveDir, "temp.jpg");
file0.delete();
if (!file0.exists()) {
try {
file0.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return;
}
}
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file0));
startActivityForResult(intent, 999);
} else {
}
}
/***
* 从相册中取图片
*/
private void pickPhoto() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");// 相片类型
startActivityForResult(intent, 888);
}
@SuppressWarnings("unused")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap photo = null;
if (requestCode == 999) {
if (file0 != null && file0.exists()) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
path = file0.getPath();
photo = BitmapFactory.decodeFile(path, options);
}
} else if (requestCode == 888) {
Uri uri = data.getData();
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = MainActivity.this.managedQuery(uri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
path = cursor.getString(column_index);
if (photo != null)// 如果不释放的话,不断取图片,将会内存不够
photo.recycle();
photo = BitmapFactory.decodeFile(path);
}
file0 = new File(path);
imageView.setImageBitmap(photo);
}
}
com.example.demo.SelectPicPopupWindow
package com.example.demo;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
public class SelectPicPopupWindow extends PopupWindow {
private Button btn_take_photo, btn_pick_photo, btn_cancel;
private View mMenuView;
public SelectPicPopupWindow(Activity context, OnClickListener itemsOnClick) {
super(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMenuView = inflater.inflate(R.layout.alert_dialog, null);
btn_take_photo = (Button) mMenuView.findViewById(R.id.btn_take_photo);
btn_pick_photo = (Button) mMenuView.findViewById(R.id.btn_pick_photo);
btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
// 鍙栨秷鎸夐挳
btn_cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 閿?瘉寮瑰嚭妗?
dismiss();
}
});
// 璁剧疆鎸夐挳鐩戝惉
btn_pick_photo.setOnClickListener(itemsOnClick);
btn_take_photo.setOnClickListener(itemsOnClick);
// 璁剧疆SelectPicPopupWindow鐨刅iew
this.setContentView(mMenuView);
// 璁剧疆SelectPicPopupWindow寮瑰嚭绐椾綋鐨勫
this.setWidth(LayoutParams.FILL_PARENT);
// 璁剧疆SelectPicPopupWindow寮瑰嚭绐椾綋鐨勯珮
this.setHeight(LayoutParams.WRAP_CONTENT);
// 璁剧疆SelectPicPopupWindow寮瑰嚭绐椾綋鍙偣鍑?
this.setFocusable(true);
// 璁剧疆SelectPicPopupWindow寮瑰嚭绐椾綋鍔ㄧ敾鏁堟灉
// this.setAnimationStyle(R.style.AnimBottom);
// 瀹炰緥鍖栦竴涓狢olorDrawable棰滆壊涓洪?鏄?
ColorDrawable dw = new ColorDrawable(0x00000000);
// 璁剧疆SelectPicPopupWindow寮瑰嚭绐椾綋鐨勮儗鏅?
this.setBackgroundDrawable(dw);
// mMenuView娣诲姞OnTouchListener鐩戝惉鍒ゆ柇鑾峰彇瑙﹀睆浣嶇疆濡傛灉鍦ㄩ?鎷╂澶栭潰鍒欓攢姣佸脊鍑烘
mMenuView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int height = mMenuView.findViewById(R.id.pop_layout).getTop();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_UP) {
if (y < height) {
dismiss();
}
}
return true;
}
});
}
}
不要忘了
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>