SHARE
TWEET

Untitled

a guest Jun 17th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 카메라 기능
  2. private void selectPhoto() {
  3.     String state = Environment.getExternalStorageState();
  4.     // 외장메모리 검사
  5.     if (Environment.MEDIA_MOUNTED.equals(state)) {
  6.         Intent intent_photo = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  7.         if (intent_photo.resolveActivity(getPackageManager()) != null) {
  8.             File photoFile = null;
  9.             try {
  10.                 photoFile = createImageFile();
  11.             } catch (IOException ex) {
  12.                 Log.e("selectPhoto Error", ex.toString());
  13.             }
  14.             if (photoFile != null) {
  15.                 photoUri = FileProvider.getUriForFile(this, "com.example.allerger", photoFile);
  16.                 intent_photo.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
  17.                 startActivityForResult(intent_photo, CAMERA_CODE);
  18.             }
  19.         }
  20.     } else {
  21.         Toast.makeText(this, "저장공간이 접근 불가능한 기기입니다", Toast.LENGTH_SHORT).show();
  22.         return;
  23.     }
  24. }
  25.  
  26. // 카메라로 찍은 사진 파일 생성
  27. private File createImageFile() throws IOException {
  28.     File dir = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera/");
  29.     if (!dir.exists()) {
  30.         dir.mkdirs();
  31.     }
  32.     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  33.     mImageCaptureName = timeStamp + ".jpg";
  34.  
  35.     File storageDir = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/DCIM/Camera/"
  36.             + mImageCaptureName);
  37.     currentPhotoPath = storageDir.getAbsolutePath();
  38.     return storageDir;
  39. }
  40.  
  41. // 각 Intent 결과 처리
  42. @Override
  43. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  44.     super.onActivityResult(requestCode, resultCode, data);
  45.     if (resultCode == RESULT_OK) {
  46.         switch (requestCode) {
  47.             '''
  48.             '''
  49.             case CAMERA_CODE:
  50.                 if (data != null) {
  51.                     Intent intent_result_camera = new Intent(MainActivity.this, ResultActivity.class);
  52.                     intent_result_camera.putExtra("path", currentPhotoPath);
  53.                     startActivity(intent_result_camera);
  54.                 } else{
  55.                     Toast.makeText(this, "사진을 불러오지 못했습니다", Toast.LENGTH_SHORT).show();
  56.                 }
  57.                 break;
  58.             default:
  59.                 break;
  60.         }
  61.     }
  62. }
  63.  
  64. // 사진의 절대경로 구하기
  65. private String getRealPathFromURI(Uri contentUri) {
  66.     int column_index = 0;
  67.     String[] pic = {MediaStore.Images.Media.DATA};
  68.     Cursor cursor = getContentResolver().query(contentUri, pic, null, null, null);
  69.     if (cursor.moveToFirst()) {
  70.         column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  71.     }
  72.     return cursor.getString(column_index);
  73. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top