[android] カメラとギャラリー、それぞれから画像を取得
カメラとギャラリー、それぞれから画像を取得
カメラとギャラリーの選択肢を表示し、ユーザが選択した画像をImageViewに表示させる
Manifest.xml
Activity.java
*ChooserにGallaryのIntentとCameraのIntentを登録
*onActivityResultで結果を受け取って画像表示している
private ImageButton closeButton; private ImageButton photoButton; private Uri resultUri; private static final int REQUEST_CHOOSER = 1000; private final static int REQUEST_PERMISSION = 1002; private String filePath; private Uri cameraUri; private File cameraFile; private Intent intentCamera; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); photoButton = (ImageButton)findViewById(R.id.photo_button); photoButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { _showGallery(); //_showGalleryメソッドを起動 } }); } private void _showGallery() { //カメラの起動Intentの用意 if (Build.VERSION.SDK_INT >= 23) { checkPermission(); } else { intentCamera = cameraIntent(); //cameraIntentというIntentを返す } // ギャラリー用のIntent作成 Intent intentGallery; if (Build.VERSION.SDK_INT < 19) { intentGallery = new Intent(Intent.ACTION_GET_CONTENT); intentGallery.setType("image/*"); } else { intentGallery = new Intent(Intent.ACTION_OPEN_DOCUMENT); intentGallery.addCategory(Intent.CATEGORY_OPENABLE); intentGallery.setType("image/jpeg"); } //ChooserにGallaryのIntentとCameraのIntentを登録 Intent intent = Intent.createChooser(intentGallery, "Select Image"); if(intentCamera!=null){ intent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {intentCamera}); } startActivityForResult(intent, REQUEST_CHOOSER); } // @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == REQUEST_CHOOSER) { if(resultCode != RESULT_OK) { // キャンセル時 return ; } //dataがnullの場合はギャラリーではなくカメラからの取得と判定しカメラのUriを使う resultUri = (data != null ? data.getData() : cameraUri); if(resultUri == null) { // 取得失敗 Toast.makeText(this, "Error.Try again.", Toast.LENGTH_LONG).show(); return; } // ギャラリーへスキャンを促す MediaScannerConnection.scanFile( this, new String[]{resultUri.getPath()}, new String[]{"image/jpeg"}, null ); // 画像を設定 ImageView imageView = (ImageView)findViewById(R.id.photo); imageView.setImageURI(resultUri); } } private Intent cameraIntent(){ // 保存先のフォルダーを作成 File cameraFolder = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "IMG" ); cameraFolder.mkdirs(); // 保存ファイル名 String fileName = new SimpleDateFormat("ddHHmmss").format(new Date()); filePath = cameraFolder.getPath() +"/" + fileName + ".jpg"; Log.d("debug","filePath:"+filePath); // capture画像のファイルパス cameraFile = new File(filePath); cameraUri = Uri.fromFile(cameraFile); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraUri); return intent; } // Runtime Permission check private void checkPermission(){ // 既に許可している if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED){ cameraIntent(); } // 拒否していた場合 else{ requestLocationPermission(); } } // 許可を求める private void requestLocationPermission() { if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { ActivityCompat.requestPermissions(ClubCreationImageActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION); } else { Toast toast = Toast.makeText(this, "Camera function is disabled", Toast.LENGTH_SHORT); toast.show(); ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,}, REQUEST_PERMISSION); } }
*インターネット上で公開されているコードを利用したが、カメラIntentを作成する度に
謎の画像がギャラリーに登録されて増殖するので手を加えた