2019 May 25.
"AndroidManifest.xml"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="YOUR.PACKAGE.sampletakephotogetimagefile">
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
"AndroidManifest.xml"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="YOUR.PACKAGE.sampletakephotogetimagefile">
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
package YOUR.PACKAGE.sampletakephotogetimagefile; import android.Manifest; import android.annotation.TargetApi; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Intent; import android.content.pm.PackageManager; import android.database.Cursor; import android.net.Uri; import android.os.Build; import android.provider.MediaStore; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.format.DateFormat; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { Button button_take_photo; ImageView image_view; TextView text_view_uri_path, text_view_file_path, text_view_image_title; static final int REQUEST_CAPTURE_IMAGE = 100; private final int REQUEST_PERMISSION__EX_STORAGE = 1000; private final int REQUEST_PERMISSION_CAMERA = 1010; Uri pictureUri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViews(); // check & get permissions if(Build.VERSION.SDK_INT >= 23){ checkPermission(); } if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){ button_take_photo.setText(R.string.button_unable_phote ); button_take_photo.setEnabled(false); } setListeners(); } protected void findViews(){ button_take_photo = findViewById(R.id.button_take_photo); image_view = findViewById(R.id.imageView); text_view_uri_path = findViewById(R.id.textViewUriPath); text_view_file_path = findViewById(R.id.textViewFilePath); text_view_image_title = findViewById(R.id.textViewImageTitle); } // Permissionの確認 // from https://akira-watson.com/android/mediastore.html @TargetApi(Build.VERSION_CODES.M) public void checkPermission() { if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){ requestCameraPermission(); } if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ requestExStoragePermission(); } } // CameraPermission許可を求める @TargetApi(Build.VERSION_CODES.M) private void requestCameraPermission() { if (shouldShowRequestPermissionRationale( Manifest.permission.CAMERA)) { requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_PERMISSION_CAMERA); } else { Toast toast = Toast.makeText(this, "許可されないとアプリが実行できません", Toast.LENGTH_SHORT); toast.show(); requestPermissions(new String[]{Manifest.permission.CAMERA }, REQUEST_PERMISSION_CAMERA); } } // ExternalStoragePermission許可を求める @TargetApi(Build.VERSION_CODES.M) private void requestExStoragePermission() { if (shouldShowRequestPermissionRationale( Manifest.permission.WRITE_EXTERNAL_STORAGE)) { requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION__EX_STORAGE); } else { Toast toast = Toast.makeText(this, "許可されないとアプリが実行できません", Toast.LENGTH_SHORT); toast.show(); requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE }, REQUEST_PERMISSION__EX_STORAGE); } } // requestPermission結果に対する処理 @Override public void onRequestPermissionsResult( int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == REQUEST_PERMISSION_CAMERA) { if (grantResults[0] != PackageManager.PERMISSION_GRANTED) { // 拒否された時の対応 Toast toast = Toast.makeText(this, "これ以上なにもできません", Toast.LENGTH_SHORT); toast.show(); } } else if (requestCode == REQUEST_PERMISSION__EX_STORAGE) { if (grantResults[0] != PackageManager.PERMISSION_GRANTED) { // 拒否された時の対応 Toast toast = Toast.makeText(this, "これ以上なにもできません", Toast.LENGTH_SHORT); toast.show(); } } else { Toast toast = Toast.makeText(this, "Bad requestCode", Toast.LENGTH_SHORT); toast.show(); } } protected void setListeners(){ // カメラ起動ボタンが押された時 button_take_photo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { /* 写真用情報を収集し、ContentResolverを使ってandroidに備わっている MediaStore.Images.Mediaデータベースに写真用情報を追加し、そのURI を取得する */ long dateTaken = System.currentTimeMillis(); String filename = DateFormat.format("yyyy-MM-dd_kk:mm:ss", dateTaken).toString() + ".jpg"; ContentResolver contentResolver = getContentResolver(); ContentValues values = new ContentValues(5); values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); values.put(MediaStore.Images.Media.DATE_MODIFIED, System.currentTimeMillis()/1000); values.put(MediaStore.Images.Media.TITLE, filename); values.put(MediaStore.Images.Media.DISPLAY_NAME, filename); values.put(MediaStore.Images.Media.DATE_TAKEN,System.currentTimeMillis()); pictureUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); // カメラを起動する Intent intent = new Intent(); intent.setAction("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri); // MediaStore.EXTRA_OUTPUTで指定したpictureUriに、撮影後に画像データが書き込まれる startActivityForResult(intent, REQUEST_CAPTURE_IMAGE); } }); } // このアクティビティから起動された他の機能から戻ってきた時の処理 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data ) { /* 正しい結果が得られなかった場合の処理 撮影キャンセルなどするとこっちに来る */ if (resultCode != RESULT_OK) { // カメラ撮影からの復帰の時 if (requestCode == REQUEST_CAPTURE_IMAGE) { Toast toast = Toast.makeText(this,"resultCode != RESULT_OK", Toast.LENGTH_LONG); toast.show(); return; } } /* * 写真撮影できた場合 */ if (requestCode == REQUEST_CAPTURE_IMAGE) { if (data == null ) { Toast.makeText(this, "data == null ", Toast.LENGTH_SHORT).show(); } else if ( data.getData() == null) { Toast.makeText(this, "data.getData() == null",Toast.LENGTH_SHORT).show(); } // 撮影した写真を取り出す image_view.setImageURI(pictureUri); text_view_uri_path.setText("Uri path: " + pictureUri.getPath()); // get filepath & title from uri ContentResolver contentResolver = this.getContentResolver(); String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media.TITLE }; Cursor cursor = contentResolver.query(pictureUri, columns, null, null, null); if(cursor == null) return; int pathIndex = cursor.getColumnIndex( MediaStore.Images.Media.DATA); int titleIndex = cursor.getColumnIndex( MediaStore.Images.Media.TITLE); cursor.moveToFirst(); String filePath = cursor.getString(pathIndex); String imageTitle = cursor.getString(titleIndex); text_view_file_path.setText(filePath); text_view_image_title.setText(imageTitle); return; } } }
0 件のコメント:
コメントを投稿