80,441
社区成员
发帖
与我相关
我的任务
分享
/**
* 从照片文件夹中选择完图片后会调用这个方法
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK)
return;
switch (requestCode) {
case PHOTO_PICKED_WITH_DATA: {// 调用(相册)Gallery返回的
try {
// 获得图片的uri
originalUri = data.getData();
} catch (Exception e) {
System.out.println(e.getMessage());
}
// 获取路径---------------------------
String[] proj = { MediaStore.Images.Media.DATA };
// 好像是android多媒体数据库的封装接口,具体的看Android文档
Cursor cursor = managedQuery(originalUri, proj, null, null, null);
// 按我个人理解 这个是获得用户选择的图片的索引值
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
// 将光标移至开头 ,这个很重要,不小心很容易引起越界
cursor.moveToFirst();
// 最后根据索引值获取图片路径
userSelectPath = cursor.getString(column_index);
if (userSelectPath.indexOf("/") != -1) {
imgName = userSelectPath.substring(userSelectPath
.lastIndexOf("/") + 1);
}
try {
BitmapFactory.Options options = new BitmapFactory.Options();
// 先设置为TRUE不加载到内存中,但可以得到宽和高
options.inJustDecodeBounds = true;
bm = BitmapFactory.decodeFile(userSelectPath, options); // 此时返回bm为空
options.inJustDecodeBounds = false;
// 计算缩放比
int be = (int) (options.outHeight / (float) 200);
if (be <= 0)
be = 1;
options.inSampleSize = be;
// 这样就不会内存溢出了
Bitmap bm = BitmapFactory.decodeFile(userSelectPath, options);
Handler h=new Handler();
h.post(new Runnable() {
@Override
public void run() {
rl_send_pic.setVisibility(View.VISIBLE);
img_send_pic.setImageBitmap(bm);
isImg = true;
// bm = null;
}
});
} catch (Exception e) {
}
break;
}
// 照相机程序返回的,再次调用图片剪辑程序去修剪图片
case CAMERA_WITH_DATA: {
try {
userSelectPath = PHOTO_DIR + "/" + picName;
imgName = picName;
BitmapFactory.Options options = new BitmapFactory.Options();
// 先设置为TRUE不加载到内存中,但可以得到宽和高
options.inJustDecodeBounds = true;
bm = BitmapFactory.decodeFile(userSelectPath, options); // 此时返回bm为空
options.inJustDecodeBounds = false;
// 计算缩放比
int be = (int) (options.outHeight / (float) 200);
if (be <= 0)
be = 1;
options.inSampleSize = be;
// 这样就不会内存溢出了
bm = BitmapFactory.decodeFile(userSelectPath, options);
h.post(new Runnable() {
@Override
public void run() {
rl_send_pic.setVisibility(View.VISIBLE);
img_send_pic.setImageBitmap(bm);//放预览图的控件
isImg = true;
}
});
} catch (Exception e) {
System.out.println("e=" + e);
}
break;
}
}
}
public class MyCameraActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}