public class MainActivity extends AppCompatActivity {
static final int PERMISSION_REQUEST_CONTACTS = 1001;
static final int REQUEST_TAKE_PHOTO = 2001;
ImageView imageView;
String currentPhotoPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] permissionRequests = {
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
checkPermissions(permissionRequests);
imageView = findViewById(R.id.imageView);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException e) {
e.printStackTrace();
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(getApplicationContext(),
"com.example.myapplication.fileprovider", photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(intent, REQUEST_TAKE_PHOTO);
}
}
}
});
}
public void checkPermissions(String[] permissionRequests) {
final ArrayList<String> permissionRequestList = new ArrayList<String>();
for (final String request : permissionRequests) {
if (ContextCompat.checkSelfPermission(this, request) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, request)) { // Redundant in this case
permissionRequestList.add(request);
} else {
permissionRequestList.add(request);
}
}
}
if (!permissionRequestList.isEmpty()) {
final String[] results = new String[permissionRequestList.size()];
permissionRequestList.toArray(results);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("info");
String msg = "This app won't work properly unless you grant below permissions.";
for (String str : results)
msg += ("\n- "+ str);
builder.setMessage(msg);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MainActivity.this, results, PERMISSION_REQUEST_CONTACTS);
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CONTACTS: {
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, permissions[i] + " permission granted.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, permissions[i] + " permission denied.", Toast.LENGTH_SHORT).show();
}
}
}
}
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss").format(new Date());
String imageFileName = "IMAGE_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
// If you saved your photo to the directory provided by getExternalFilesDir(),
// the media scanner cannot access the files because they are private to your app.
File image = File.createTempFile(imageFileName, ".jpg", storageDir);
currentPhotoPath = image.getAbsolutePath();
return image;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
Bitmap originalBitmap = BitmapFactory.decodeFile(currentPhotoPath);
int scaleFactor = Math.max(originalBitmap.getWidth()/imageView.getWidth(), originalBitmap.getHeight()/imageView.getHeight());
if (scaleFactor < 1)
scaleFactor = 1;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(originalBitmap, originalBitmap.getWidth()/scaleFactor,
originalBitmap.getHeight()/scaleFactor, true);
Matrix m = new Matrix();
m.postRotate(90);
Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), m, true);
imageView.setImageBitmap(rotatedBitmap);
}
}
}