拍照的
Java代码
btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File out = new File(Environment.getExternalStorageDirectory(), "camera.png"); Uri uri = Uri.fromFile(out); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); xxActivity.startActivityForResult(intent, 3); } });
相册
Java代码
btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 80); intent.putExtra("outputY", 80); intent.putExtra("return-data", true); xxActivity.startActivityForResult(intent, 2); } });
处理的activity
Java代码
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != RESULT_OK) { return; } else { switch (requestCode) { case 2: Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data"); Home.personalinfo.headbmp = cameraBitmap; try { String str = saveMyBitmap(cameraBitmap); } catch (IOException e) { e.printStackTrace(); } break; /* 拍照后保存图片,并跳到裁剪功能 */ case 3: new saveAsy().execute(""); break; } } super.onActivityResult(requestCode, resultCode, data); }
saveAsy的方法
Java代码
class saveAsy extends AsyncTask<String, Object, String> { @Override protected String doInBackground(String... str) { dispathBitmap(); Intent intent = new Intent("com.android.camera.action.CROP"); try { intent.setData(Uri .parse(android.provider.MediaStore.Images.Media .insertImage(getContentResolver(), storageUrl+"/camera.png", null, null))); } catch (FileNotFoundException e) { e.printStackTrace(); } intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 80); intent.putExtra("outputY", 80); intent.putExtra("return-data", true); startActivityForResult(intent, 2); return ""; } @Override protected void onPostExecute(String bmp) { super.onPostExecute(bmp); } }