//varibles
necesarias para manejo de fotos
File xphotofile;
String name;
Bitmap bitmap;
************************************
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.PROYECTONAME.provider"(digitamos provider)
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"(digitamos FILE_PROVIDER_PATHS)
android:resource="@xml/file_paths">
</meta-data>
</provider>
<paths xmlns:android="http://schemas.android.com/apk/res/android" >
<external-path
name="my_images"
path="Android/data/com.example.camaraaccion/files/Pictures" />
</paths>
if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.CAMERA}, 1000);
}
*****************************************************
//metodo tomar foto
static final int REQUEST_TAKE_PHOTO = 1;
public void tomarFoto(View view)
{
//Levanta la camara
Toast.makeText(MainActivity.this,"Levantando la camara", Toast.LENGTH_SHORT).show();
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
Toast.makeText(MainActivity.this,"Creando Archivo", Toast.LENGTH_SHORT).show();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this, "com.example.PROYECTONAME.provider", photoFile);
xphotofile= photoFile;
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}**************************************************
//metodo crear archivo
String currentPhotoPath;
private
File createImageFile() throws IOException
{
//
Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "Backup_" + timeStamp + "_";
File storageDir1 =
getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(imageFileName, ".jpg",storageDir1);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath =
image.getAbsolutePath();
name=currentPhotoPath;
return
image;
}
*********************************************
//vista previa en el Imview
static final int REQUEST_IMAGE_CAPTURE =1;
@Override
protected void onActivityResult(int requestCode, int
resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if
(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
//Trasnforma
la foto xphotofile a bitmap y muestra en el view
Bitmap bitmap = BitmapFactory.decodeFile(xphotofile.getAbsolutePath());
fotopreview.setImageBitmap(bitmap);
//
guardarFotoGallery();
btncamara.setEnabled(false);
}
else
{
Toast.makeText(MainActivity.this,"Cancelo el guardado de la foto", Toast.LENGTH_SHORT).show();
}
}