Android呼び出しシステムカメラ撮影
6045 ワード
public class MainActivity extends Activity {
private Button btn_bySysCamera, btn_bySysVideoCamera;
ImageView iv_CameraImg;
private Uri mCameraUri;
File photoFile = null;
String storagePath;
String authority = "com.example.myapplication.fileprovider";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_bySysCamera = (Button) findViewById(R.id.btn_bySysCamera);
btn_bySysVideoCamera = (Button) findViewById(R.id.btn_bySysVideoCamera);
iv_CameraImg = (ImageView) findViewById(R.id.iv_CameraImg);
btn_bySysCamera.setOnClickListener(click);
btn_bySysVideoCamera.setOnClickListener(click);
}
private View.OnClickListener click = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent captureIntent = null;
switch (v.getId()) {
case R.id.btn_bySysCamera:
captureIntent = new Intent();
// // Action
captureIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.addCategory(Intent.CATEGORY_DEFAULT);
//
Uri photoUri = null;
// if (isAndroidQ) {
// photoUri = createImageUri(); // android 10
// } else {
try {
photoFile = getFileFromCamera();
} catch (Exception e) {
e.printStackTrace();
}
if (photoFile != null) {
mCameraImagePath = photoFile.getAbsolutePath();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
captureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//
// Android 7.0 , FileProvider content Uri
photoUri = FileProvider.getUriForFile(MainActivity.this, authority, photoFile);
} else {
photoUri = Uri.fromFile(photoFile);
}
}
// }
mCameraUri = photoUri;
if (photoUri != null) {
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);//
captureIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//
captureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
startActivityForResult(captureIntent, 1);
break;
// case R.id.btn_bySysVideoCamera:
// intent = new Intent(MainActivity.this, SysVideoCameraActivity.class);
// startActivity(intent);
// break;
default:
break;
}
}
};
/**
* ,Android 10
*/
private String mCameraImagePath;
private File getFileFromCamera() {
File imageFile = null;
File storageDir;
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
try {
storagePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
storageDir = new File(storagePath);
storageDir.mkdirs();
imageFile = File.createTempFile(timeStamp, ".jpg", storageDir);
} catch (IOException e) {
e.printStackTrace();
}
return imageFile;
}
Bitmap bitmap;
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {//
if (photoFile != null && photoFile.exists()) {
try {
bitmap = MediaStore.Images.Media.getBitmap(MainActivity.this.getContentResolver(), mCameraUri);//Uri bitmap
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);//
//
MediaStore.Images.Media.insertImage(MainActivity.this.getContentResolver(), bitmap, "Title", null);
Intent localIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mCameraUri);
MainActivity.this.sendBroadcast(localIntent);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
xml