调用系统拍照和相册的集成工具类(包含Popwindow的使用):
package com.example.takevideo;
import java.io.File;
import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Handler;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
public class PhotoPopWindow {
private String TAG = "MyPopWindow";
private Context mContext;
private PopupWindow pop;
private RelativeLayout parent;
public Button bt1;
public Button bt2;
public Button bt3;
/** 拍的照片文件 */
private File take_photo_file;
public PhotoPopWindow(Context context) {
mContext = context;
initPopView();
initPopListener();
}
public PopupWindow getPop() {
return pop;
}
public void initPopView() {
pop = new PopupWindow(mContext);
// 设置pop的布局视图
View view = ((Activity) mContext).getLayoutInflater().inflate(
R.layout.my_popwindow, null);
// 设置pop的属性
pop.setWidth(LayoutParams.MATCH_PARENT);
pop.setHeight(LayoutParams.MATCH_PARENT);
pop.setBackgroundDrawable(new BitmapDrawable());
pop.setFocusable(true);
pop.setOutsideTouchable(true);
pop.setContentView(view);
parent = (RelativeLayout) view.findViewById(R.id.parent);
bt1 = (Button) view.findViewById(R.id.my_popwindow_camera);
bt2 = (Button) view.findViewById(R.id.my_popwindow_photo);
bt3 = (Button) view.findViewById(R.id.my_popwindow_cancel);
}
private void initPopListener() {
parent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pop.dismiss();
}
});
bt1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
pop.dismiss();
LogUtil.i(TAG, "照相");
// 照相
// 拍照的临时缓存
File destDir = new File(Constants.IMAGES_DIR);
if (!destDir.exists()) {
destDir.mkdirs();
}
// 拍得照片的路径
String take_photo_path = Constants.IMAGES_DIR
+ System.currentTimeMillis() + ".png";
take_photo_file = new File(take_photo_path);
if (!take_photo_file.exists()) {
try {
// 在指定的文件夹中创建文件
take_photo_file.createNewFile();
} catch (Exception e) {
}
}
Uri imageUri = Uri.fromFile(take_photo_file);
Intent openCameraIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
// 指定照片保存路径(SD卡),image.png为一个临时文件,每次拍照后这个图片都会被替换
if (imageUri != null) {
openCameraIntent
.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
}
((Activity) mContext).startActivityForResult(openCameraIntent,
Constants.CAMERA_WITH_DATA);
}
});
bt2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
pop.dismiss();
// 相册
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
((Activity) mContext).startActivityForResult(intent,
Constants.PHOTO_PICKED_WITH_DATA);
}
});
bt3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 取消
pop.dismiss();
}
});
}
/**
* 返回拍照的或相册中的照片文件
*
* @param requestCode
* @param resultCode
* @param data
* @return
*/
public File INonActivityResult(int requestCode, int resultCode, Intent data) {
LogUtil.i(TAG, "onActivityResult");
switch (requestCode) {
case Constants.CAMERA_WITH_DATA: {
// 照相机程序返回的
// return ImageUtil.rotateImage(take_photo_file.getAbsoluteFile());
return take_photo_file;
}
case Constants.PHOTO_PICKED_WITH_DATA: {
if (data == null) {
return null;
}
// 照片的原始资源地址
Uri uri_picture = data.getData();
return new File(getImagePath(uri_picture));
}
default:
break;
}
return null;
}
/**
* 得到相册中图片的路径
*
* @return
*/
public String getImagePath(Uri originalUri) {
String[] proj = { MediaStore.Images.Media.DATA };
// 好像是android多媒体数据库的封装接口,具体的看Android文档
Cursor cursor = ((Activity) mContext).managedQuery(originalUri, proj,
null, null, null);
// 按我个人理解 这个是获得用户选择的图片的索引值
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
// 将光标移至开头 ,这个很重要,不小心很容易引起越界
cursor.moveToFirst();
// 最后根据索引值获取图片路径
String path = cursor.getString(column_index);
return path;
}
/**
* 调用系统的裁剪
*
* @param uri
*/
public void cropPhoto(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", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
((Activity) mContext).startActivityForResult(intent,
Constants.CROP_PICKED_WITH_DATA);
}
}