1. 程式人生 > >彈出popupwindow選擇拍照相簿

彈出popupwindow選擇拍照相簿

<pre name="code" class="java">效果圖:點選新增圖片,彈出popupwindow
<img src="https://img-blog.csdn.net/20140910171230459?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzE4MzEzOQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
點選從相簿選擇,ok,you know,and so

package com.example.popupwindow;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

	private PopupWindow mPopupWindowDialog;
	private TextView mTextViewDialogTakePicture, mTextViewDialogAlbum,
			mTextViewDialogCancel;

	private String takePicturePath = "";

	private Button mImageViewAddThumb;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.post_daily_picture_choose_dialog,
				null);
		mTextViewDialogTakePicture = (TextView) view
				.findViewById(R.id.textview_dialog_take_picture);
		mTextViewDialogAlbum = (TextView) view
				.findViewById(R.id.textview_dialog_album);
		mTextViewDialogCancel = (TextView) view
				.findViewById(R.id.textview_dialog_cancel);
		mPopupWindowDialog = new PopupWindow(view, LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);
		mPopupWindowDialog.setFocusable(true);
		mPopupWindowDialog.update();
		mPopupWindowDialog.setBackgroundDrawable(new BitmapDrawable());
		mPopupWindowDialog.setOutsideTouchable(true);
		mTextViewDialogTakePicture.setOnClickListener(this);
		mTextViewDialogAlbum.setOnClickListener(this);
		mTextViewDialogCancel.setOnClickListener(this);
		mImageViewAddThumb = (Button) findViewById(R.id.imageview_add_thumb);
		mImageViewAddThumb.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imageview_add_thumb:
			if (mPopupWindowDialog != null && !mPopupWindowDialog.isShowing())
				mPopupWindowDialog.showAtLocation(
						findViewById(R.id.imageview_add_thumb), Gravity.BOTTOM
								| Gravity.CENTER_HORIZONTAL, 0, 0);
			break;
		case R.id.textview_dialog_take_picture:// 拍照
			try {
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				startActivityForResult(intent, 1);
			} catch (Exception e) {
			}
			if (mPopupWindowDialog != null && mPopupWindowDialog.isShowing()) {
				mPopupWindowDialog.dismiss();
			}
			break;

		case R.id.textview_dialog_album:// 相簿
			try {
				// 使用Intent
				Intent intent = new Intent(Intent.ACTION_VIEW);
				intent.setType("image/*");
				startActivity(intent);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (mPopupWindowDialog != null && mPopupWindowDialog.isShowing()) {
				mPopupWindowDialog.dismiss();
			}
			break;

		case R.id.textview_dialog_cancel: // 取消
			if (mPopupWindowDialog != null && mPopupWindowDialog.isShowing()) {
				mPopupWindowDialog.dismiss();
			}
			break;

		}

	}

}
附加個人專案下載地址:http://download.csdn.net/detail/u013183139/7891531