private String[] items = new String[] { "图库", "拍照" }; private static final int IMAGE_REQUEST_CODE = 0; private static final int CAMERA_REQUEST_CODE = 1; private static final int RESULT_REQUEST_CODE = 2; public void headphoto(View view) { new AlertDialog.Builder(this) .setTitle("设置头像") .setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: Intent intentFromGallery = new Intent(); intentFromGallery.setType("p_w_picpath/*"); // 设置文件类型 intentFromGallery .setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(intentFromGallery, IMAGE_REQUEST_CODE); break; case 1: Intent intent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri .fromFile(new File(Environment .getExternalStorageDirectory()+ "/head/", "temp.jpg"))); startActivityForResult(intent, CAMERA_REQUEST_CODE); break; } } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).show(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // 结果码不等于取消时候 if (resultCode != RESULT_CANCELED) { switch (requestCode) { case IMAGE_REQUEST_CODE: startPhotoZoom(data.getData()); break; case CAMERA_REQUEST_CODE: File picture = new File( Environment.getExternalStorageDirectory() + "/head/temp.jpg"); startPhotoZoom(Uri.fromFile(picture)); break; case RESULT_REQUEST_CODE: if (data != null) { getImageToView(data); } break; } } super.onActivityResult(requestCode, resultCode, data); } /** * 裁剪图片方法实现 * * @param uri */ public void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "p_w_picpath/*"); // 设置裁剪 intent.putExtra("crop", "true"); // aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY 是裁剪图片宽高 intent.putExtra("outputX", 320); intent.putExtra("outputY", 320); intent.putExtra("return-data", true); startActivityForResult(intent, 2); } /** * 保存裁剪之后的图片数据 * * @param picdata */ private void getImageToView(Intent data) { Bundle extras = data.getExtras(); if (extras != null) { Bitmap photo = extras.getParcelable("data"); Drawable drawable = new BitmapDrawable(photo); head.setImageDrawable(drawable); savePic(photo); } } /** * @param bitmap * @throws 保存图片到本地 */ public void savePic(Bitmap b) { FileOutputStream fos = null; try { String path = (Environment.getExternalStorageDirectory() .getAbsolutePath() + "/head/"); File file = new File(path); if (!file.exists()) { file.mkdirs(); } File f = new File(path, "head.png"); if (f.exists()) { f.delete(); } fos = new FileOutputStream(f); if (null != fos) { b.compress(Bitmap.CompressFormat.PNG, 90, fos); fos.flush(); fos.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * @param 读取照片 * @return */ public static Bitmap getDiskBitmap(String pathString) { Bitmap bitmap = null; try { File file = new File(pathString); if (file.exists()) { bitmap = BitmapFactory.decodeFile(pathString); } } catch (Exception e) { } return bitmap; } String path = (Environment.getExternalStorageDirectory() .getAbsolutePath() + "/bbpet/head/head.png"); Bitmap bitmap = MyDataActivity.getDiskBitmap(path); if (bitmap != null) { headimg.setImageBitmap(bitmap); } else { headimg.setBackgroundResource(R.drawable.ic_launcher); }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。