I\'m trying to make a basic camera application that can access the saved photo from the gallery (needed as part of another app but due to problems I\'ve been having I am dev
Try this
public void onLaunchCamera(View view) {
//btn = (Button) findViewById(R.id.button);
if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
checkPermission();
}
else {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getPhotoFileUri(photoFileName)); // set the image file name
if (intent.resolveActivity(getPackageManager()) != null) {
// Start the image capture intent to take photo
startActivityForResult(intent, 0);
}
}
} else {
Toast.makeText(MainActivity.this, "No Camera",
Toast.LENGTH_LONG).show();
}
}
private void checkPermission() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {//Can add more as per requirement
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA},
123);
} else {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,getPhotoFileUri(photoFileName)); // set the image file name
if (intent.resolveActivity(getPackageManager()) != null) {
// Start the image capture intent to take photo
startActivityForResult(intent, 0);
}
}
And make sure you have set proper version in your build.gradle
**compileSdkVersion 23
buildToolsVersion "23.0.2"**
defaultConfig {
applicationId "your_package_name"
minSdkVersion 15
**targetSdkVersion 23**
versionCode 1
versionName "1.0"
multiDexEnabled true
}