Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.67 KB | None | 0 0
  1. public class FotografiaActivity extends AppCompatActivity {
  2. EditText etUsuario, etNservicio;
  3. private static String APP_DIRECTORY = "MyPictureApp/";
  4. private static String MEDIA_DIRECTORY = APP_DIRECTORY + "PictureApp";
  5.  
  6. private final int MY_PERMISSIONS = 100;
  7. private final int PHOTO_CODE = 200;
  8. private final int SELECT_PICTURE = 300;
  9.  
  10. private ImageView mSetImage;
  11. private Button mOptionButton;
  12. private RelativeLayout mRlView;
  13.  
  14. private String mPath;
  15.  
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_fotografia);
  20. etUsuario=(EditText)findViewById(R.id.etUsuario);
  21. etNservicio=(EditText)findViewById(R.id.etNservicio);
  22. mSetImage = (ImageView) findViewById(R.id.set_picture);
  23. mOptionButton = (Button) findViewById(R.id.show_options_button);
  24. mRlView = (RelativeLayout) findViewById(R.id.rl_view);
  25.  
  26. Bundle extras = getIntent().getExtras();
  27. String dato= extras.getString("Usuarios").toString();
  28. etUsuario.setText(dato);
  29. String datos= extras.getString("Nservicio").toString();
  30. etNservicio.setText(datos);
  31. if(mayRequestStoragePermission())
  32. mOptionButton.setEnabled(true);
  33. else
  34. mOptionButton.setEnabled(false);
  35.  
  36.  
  37. mOptionButton.setOnClickListener(new View.OnClickListener() {
  38. @Override
  39. public void onClick(View v) {
  40. showOptions();
  41. }
  42. });
  43.  
  44. }
  45. public void checkCancelar(View arg0){
  46. Intent intent = new Intent(FotografiaActivity.this,DashboardActivity.class);
  47. Bundle extras = new Bundle();
  48. extras.putString("Usuarios",etUsuario.getText().toString());
  49. intent.putExtras(extras);
  50. startActivity(intent);
  51. FotografiaActivity.this.finish();
  52. }
  53. public void checkFinalizar(View arg0){
  54. Intent intent = new Intent(FotografiaActivity.this,UbicacionActivity.class);
  55. Bundle extras = new Bundle();
  56. extras.putString("Usuarios",etUsuario.getText().toString());
  57. extras.putString("Nservicio", etNservicio.getText().toString());
  58. intent.putExtras(extras);
  59. startActivity(intent);
  60. FotografiaActivity.this.finish();
  61. }
  62. private boolean mayRequestStoragePermission() {
  63.  
  64. if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
  65. return true;
  66.  
  67. if((checkSelfPermission(WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) &&
  68. (checkSelfPermission(CAMERA) == PackageManager.PERMISSION_GRANTED))
  69. return true;
  70.  
  71. if((shouldShowRequestPermissionRationale(WRITE_EXTERNAL_STORAGE)) || (shouldShowRequestPermissionRationale(CAMERA))){
  72. Snackbar.make(mRlView, "Los permisos son necesarios para poder usar la aplicación",
  73. Snackbar.LENGTH_INDEFINITE).setAction(android.R.string.ok, new View.OnClickListener() {
  74. @TargetApi(Build.VERSION_CODES.M)
  75. @Override
  76. public void onClick(View v) {
  77. requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE, CAMERA}, MY_PERMISSIONS);
  78. }
  79. });
  80. }else{
  81. requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE, CAMERA}, MY_PERMISSIONS);
  82. }
  83.  
  84. return false;
  85. }
  86.  
  87. private void showOptions() {
  88. final CharSequence[] option = {"Tomar foto", "Elegir de galeria", "Cancelar"};
  89. final AlertDialog.Builder builder = new AlertDialog.Builder(FotografiaActivity.this);
  90. builder.setTitle("Eleige una opción");
  91. builder.setItems(option, new DialogInterface.OnClickListener() {
  92. @Override
  93. public void onClick(DialogInterface dialog, int which) {
  94. if(option[which] == "Tomar foto"){
  95. openCamera();
  96. }else if(option[which] == "Elegir de galeria"){
  97. Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  98. intent.setType("image/*");
  99. startActivityForResult(Intent.createChooser(intent, "Selecciona app de imagen"), SELECT_PICTURE);
  100. }else {
  101. dialog.dismiss();
  102. }
  103. }
  104. });
  105.  
  106. builder.show();
  107. }
  108.  
  109. private void openCamera() {
  110. File file = new File(Environment.getExternalStorageDirectory(), MEDIA_DIRECTORY);
  111. boolean isDirectoryCreated = file.exists();
  112.  
  113. if(!isDirectoryCreated)
  114. isDirectoryCreated = file.mkdirs();
  115.  
  116. if(isDirectoryCreated){
  117. Long timestamp = System.currentTimeMillis() / 1000;
  118. String imageName = timestamp.toString() + ".jpg";
  119.  
  120. mPath = Environment.getExternalStorageDirectory() + File.separator + MEDIA_DIRECTORY
  121. + File.separator + imageName;
  122.  
  123. File newFile = new File(mPath);
  124.  
  125. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  126. intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(newFile));
  127. startActivityForResult(intent, PHOTO_CODE);
  128. }
  129. }
  130.  
  131. @Override
  132. public void onSaveInstanceState(Bundle outState) {
  133. super.onSaveInstanceState(outState);
  134. outState.putString("file_path", mPath);
  135. }
  136.  
  137. @Override
  138. protected void onRestoreInstanceState(Bundle savedInstanceState) {
  139. super.onRestoreInstanceState(savedInstanceState);
  140.  
  141. mPath = savedInstanceState.getString("file_path");
  142. }
  143.  
  144. @Override
  145. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  146. super.onActivityResult(requestCode, resultCode, data);
  147.  
  148. if(resultCode == RESULT_OK){
  149. switch (requestCode){
  150. case PHOTO_CODE:
  151. MediaScannerConnection.scanFile(this,
  152. new String[]{mPath}, null,
  153. new MediaScannerConnection.OnScanCompletedListener() {
  154. @Override
  155. public void onScanCompleted(String path, Uri uri) {
  156. Log.i("ExternalStorage", "Scanned " + path + ":");
  157. Log.i("ExternalStorage", "-> Uri = " + uri);
  158. }
  159. });
  160.  
  161.  
  162. Bitmap bitmap = BitmapFactory.decodeFile(mPath);
  163. mSetImage.setImageBitmap(bitmap);
  164. break;
  165. case SELECT_PICTURE:
  166. Uri path = data.getData();
  167. mSetImage.setImageURI(path);
  168. break;
  169.  
  170. }
  171. }
  172. }
  173.  
  174. @Override
  175. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  176. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  177.  
  178. if(requestCode == MY_PERMISSIONS){
  179. if(grantResults.length == 2 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED){
  180. Toast.makeText(FotografiaActivity.this, "Permisos aceptados", Toast.LENGTH_SHORT).show();
  181. mOptionButton.setEnabled(true);
  182. }
  183. }else{
  184. showExplanation();
  185. }
  186. }
  187.  
  188. private void showExplanation() {
  189. AlertDialog.Builder builder = new AlertDialog.Builder(FotografiaActivity.this);
  190. builder.setTitle("Permisos denegados");
  191. builder.setMessage("Para usar las funciones de la app necesitas aceptar los permisos");
  192. builder.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
  193. @Override
  194. public void onClick(DialogInterface dialog, int which) {
  195. Intent intent = new Intent();
  196. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  197. Uri uri = Uri.fromParts("package", getPackageName(), null);
  198. intent.setData(uri);
  199. startActivity(intent);
  200. }
  201. });
  202. builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
  203. @Override
  204. public void onClick(DialogInterface dialog, int which) {
  205. dialog.dismiss();
  206. finish();
  207. }
  208. });
  209.  
  210. builder.show();
  211. }
  212. }
  213.  
  214. Bitmap bm = BitmapFactory.decodeFile("/path/to/image.jpg");
  215. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  216. bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm = Bitmap
  217. byte[] b = baos.toByteArray();
  218.  
  219. String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
  220.  
  221. file_put_contents('img.png', base64_decode($_POST['imagen_base64']));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement