Created
January 23, 2018 19:19
-
-
Save kjromero/b41351ba4f64121cbd5c21ac5b157dca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Se llama cuando se hace clic en el botón para guardar la foto. | |
| */ | |
| @OnClick(R.id.accept) | |
| public void accept() { | |
| if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { | |
| // Si no tiene el permiso para guardar la imagen en el almacenamiento externo, se pide el permiso. | |
| requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_WRITE_EXTERNAL_STORAGE); | |
| } else { | |
| // Si tiene el permiso para guardar la imagen, se guarda la imagen. | |
| saveImage(); | |
| } | |
| } | |
| /** | |
| * Guarda la imagen y notifica que la imagen ha sido guardada | |
| */ | |
| private void saveImage() { | |
| Uri uri = Functions.savePublicImage(getContext(), picture); | |
| if (uri != null) { | |
| // Notifica a Activity que la imagen se guardó con éxito | |
| CameraActivity activity = (CameraActivity) getActivity(); | |
| activity.setResultOK(uri); | |
| } else { | |
| showErrorDialog("Ocurrió un error al intentar guardar la imagen. Por favor prueba de nuevo"); | |
| } | |
| } | |
| /** | |
| * Guarda la imagen en la memoria pública del dispositivo | |
| * | |
| * @param picture La imagen representada como JPEG | |
| * @return El Uri donde quedó la imagen | |
| */ | |
| public static Uri savePublicImage(Context context, byte[] picture) { | |
| try { | |
| // Obtiene el File donde va a quedar la imagen | |
| File publicFile = getPublicFile(); | |
| // Guarda la foto en la memoria del dispositivo | |
| FileOutputStream fos = new FileOutputStream(publicFile); | |
| fos.write(picture); | |
| fos.close(); | |
| String path = publicFile.getPath(); | |
| saveToMediaStore(context, path); | |
| return Uri.parse(path); | |
| } catch (IOException e) { | |
| Crashlytics.logException(e); | |
| return null; | |
| } | |
| } | |
| /** | |
| * Retorna el resultado OK al Activity padre | |
| * En el data del Intent estará el Uri de la foto que se tomó | |
| * | |
| * @param uri El URI con la ruta interna donde quedó la foto | |
| */ | |
| public void setResultOK(Uri uri) { | |
| Intent intent = new Intent(); | |
| intent.setData(uri); | |
| setResult(RESULT_OK, intent); | |
| finish(); | |
| } | |
| /** | |
| * Se llama cuando alguno de los startActivityForResult retorna una respuesta | |
| * | |
| * @param requestCode | |
| * @param resultCode | |
| * @param data | |
| */ | |
| @Override | |
| public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
| if (requestCode == REQUEST_CODE_TAKE_PICTURE) { | |
| if (resultCode == Activity.RESULT_OK) { | |
| // Si se tomó una foto con éxito, se trae la ruta donde quedó guardada la foto | |
| Uri uri = data.getData(); | |
| String path = uri.getPath(); | |
| // Inicia el Job que cargará la evidencia a Amazon S3 y notificará al API | |
| startUploadEvidenceJob(path); | |
| // Luego inserta la evidencia en la base de datos | |
| insertEvidence(path); | |
| // Muestra la evidencia en la pantalla | |
| showEvidence(path); | |
| // Vuelve a verificar los inputs | |
| checkInputs(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment