【androidJava】カメラ・ギャラリーを同じダイアログに入れて選択させる方法

  • 2019年1月14日
  • Java

javascriptでできなかったカメラとギャラリーが同じダイアログに入った機能があっさりできてしまいました。

この場合はコードを先にみてしまった方が早いと思うので端折ります。

//一番最初にカメラ・ギャラりーを識別するための定数を記述しておく
private static final int REQUEST_CHOOSER = 1000;
private void showGallery() {

    //カメラの起動Intentの用意
    String photoName = System.currentTimeMillis() + ".jpg";
    ContentValues contentValues = new ContentValues();
    contentValues.put(MediaStore.Images.Media.TITLE, photoName);
    contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
    m_uri = getContentResolver()
            .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);

    Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (intentCamera.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
            System.out.println(photoFile);
        } catch (IOException ex) {
            // Error occurred while creating the File
            Log.d("Error", ex.getMessage());
            Toast.makeText(getBaseContext(), ex.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
    // ギャラリー用の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");
    }
    Intent intent = Intent.createChooser(intentCamera, "画像の選択");
    intent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {intentGallery});
    startActivityForResult(intent, REQUEST_CHOOSER);
}
</pre>
<pre>private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "PNG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".png",         /* suffix */
            storageDir      /* directory */
    );
    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = Uri.parse(image.getAbsolutePath());
    return image;
}</pre>
<pre>

カメラ・ギャラリーから取得した画像をonActivityResultで受け取って処理をします。

 

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == REQUEST_CHOOSER) {
  //ここに書きたい処理を書く</pre>
<pre>  resultUri = (data != null ? data.getData() : m_uri);</pre>
<pre>  } 
} 

二番目の引数と定数REQUEST_CHOOSERが同じ値なので、if内に処理をしていきます。

上記の例のようにgetDataでuriを取得をすればもうご自由にuriを使うことが出来ますね。

>株式会社シーポイントラボ

株式会社シーポイントラボ

TEL:053-543-9889
営業時間:9:00~18:00(月〜金)
住所:〒432-8003
   静岡県浜松市中央区和地山3-1-7
   浜松イノベーションキューブ 315
※ご来社の際はインターホンで「316」をお呼びください

CTR IMG