您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。