0% found this document useful (0 votes)
42 views2 pages

Implementación de La Camara Android

The document discusses implementing the use of the camera and gallery in an Android app. It includes code to launch the camera or gallery on button clicks, handle the results, and save or display the images. It also covers adding necessary permissions and a FileProvider to the AndroidManifest.xml to properly handle external files and URIs.

Uploaded by

Pablo Almeida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views2 pages

Implementación de La Camara Android

The document discusses implementing the use of the camera and gallery in an Android app. It includes code to launch the camera or gallery on button clicks, handle the results, and save or display the images. It also covers adding necessary permissions and a FileProvider to the AndroidManifest.xml to properly handle external files and URIs.

Uploaded by

Pablo Almeida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Implementación de la utilización de la cámara y galería

public void onClickTakePic(View v){


switch (v.getId()){
case R.id.buttonCamara:
count = prefs.getInt("imagen",0);
count++;
image = Environment.getExternalStorageDirectory() + "/" +
"fotouser"+count+".jpg";
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Uri output = Uri.fromFile(new File(photo));
Uri output = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID,
new File(image));
intent.putExtra(MediaStore.EXTRA_OUTPUT, output);
startActivityForResult(intent, 0);
break;
case R.id.buttonGaleria:
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, 1);
break;

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 0:
if (resultCode == this.RESULT_OK) {
File fileTemp = new File(image);
if (!fileTemp.exists()) {
Toast.makeText(this,
"No se ha realizado la foto", Toast.LENGTH_SHORT)
.show();
} else {

imageViewNewContact.setImageBitmap(BitmapFactory.decodeFile(image));
}
}

break;
case 1:
if (resultCode == this.RESULT_OK) {
Uri uri = data.getData();
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = this.getContentResolver().query(uri, projection,
null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
image = cursor.getString(columnIndex); // returns null
cursor.close();

imageViewNewContact.setImageBitmap(BitmapFactory.decodeFile(image));
}
break;
}

Implementación de permisos y fileprovider en AndroidMAnifest.xml

<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-
permission>
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-
permission>

<provider
android:name="android.support.v4.content.FileProvider"
android:grantUriPermissions="true"
android:exported="false"
android:authorities="${applicationId}">

<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths"/>

</provider>

Se debe crear el archivo file_provider_paths.xml en el subdirectorio /res/xml

El cual debe contener lo siguiente:

<paths>
<cache-path name="cache" path="/" />
<files-path name="files" path="/" />
</paths>

You might also like