upload android photo

0

I have developed an activity to upload a photo from the camera or from the gallery, in addition to saving it in the device, in case of capturing it, I show it in an ImageView. The problem I have is that when I try to upload the photo from the gallery I get the recent folder appears and I can go exploring the folders but I see all the photos in gray and I can not select them. Capturing the photo from the camera does it without problems.

I pass my code to you:

public class MainActivity extends AppCompatActivity {
private final String CARPETA_RAIZ="misImagenesPrueba/";
private final String RUTA_IMAGEN=CARPETA_RAIZ+"misFotos";

final int COD_SELECCIONA=10;
final int COD_FOTO=20;

Button botonCargar;
ImageView imagen;
String path;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imagen= (ImageView) findViewById(R.id.imageView);
    botonCargar= (Button) findViewById(R.id.button);

    if(validaPermisos()){
        botonCargar.setEnabled(true);
    }else{
        botonCargar.setEnabled(false);
    }
    botonCargar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cargarImagen();
        }
    });

}

private boolean validaPermisos() {

    if(Build.VERSION.SDK_INT<Build.VERSION_CODES.M){
        return true;
    }

    if((checkSelfPermission(CAMERA)==PackageManager.PERMISSION_GRANTED)&&
            (checkSelfPermission(WRITE_EXTERNAL_STORAGE)==PackageManager.PERMISSION_GRANTED)){
        return true;
    }

    if((shouldShowRequestPermissionRationale(CAMERA)) ||
            (shouldShowRequestPermissionRationale(WRITE_EXTERNAL_STORAGE))){
        cargarDialogoRecomendacion();
    }else{
        requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE,CAMERA},100);
    }

    return false;
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if(requestCode==100){
        if(grantResults.length==2 && grantResults[0]==PackageManager.PERMISSION_GRANTED
                && grantResults[1]==PackageManager.PERMISSION_GRANTED){
            botonCargar.setEnabled(true);
        }else{
            solicitarPermisosManual();
        }
    }

}

private void solicitarPermisosManual() {
    final CharSequence[] opciones={"si","no"};
    final AlertDialog.Builder alertOpciones=new AlertDialog.Builder(MainActivity.this);
    alertOpciones.setTitle("¿Desea configurar los permisos de forma manual?");
    alertOpciones.setItems(opciones, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            if (opciones[i].equals("si")){
                Intent intent=new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri=Uri.fromParts("package",getPackageName(),null);
                intent.setData(uri);
                startActivity(intent);
            }else{
                Toast.makeText(getApplicationContext(),"Los permisos no fueron aceptados",Toast.LENGTH_SHORT).show();
                dialogInterface.dismiss();
            }
        }
    });
    alertOpciones.show();
}

private void cargarDialogoRecomendacion() {
    AlertDialog.Builder dialogo=new AlertDialog.Builder(MainActivity.this);
    dialogo.setTitle("Permisos Desactivados");
    dialogo.setMessage("Debe aceptar los permisos para el correcto funcionamiento de la App");

    dialogo.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.M)
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE,CAMERA},100);
        }
    });
    dialogo.show();
}

public void onclick(View view) {
    cargarImagen();
}

private void cargarImagen() {

    final CharSequence[] opciones={"Tomar Foto","Cargar Imagen","Cancelar"};
    final AlertDialog.Builder alertOpciones=new AlertDialog.Builder(MainActivity.this);
    alertOpciones.setTitle("Seleccione una Opción");
    alertOpciones.setItems(opciones, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            if (opciones[i].equals("Tomar Foto")){
                tomarFotografia();
            }else{
                if (opciones[i].equals("Cargar Imagen")){
                    Intent intent=new Intent(Intent.ACTION_GET_CONTENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent.setType("image/");
                    startActivityForResult(intent.createChooser(intent,"Seleccione la Aplicación"),COD_SELECCIONA);
                }else{
                    dialogInterface.dismiss();
                }
            }
        }
    });

    alertOpciones.show();

}

private void tomarFotografia() {
    File fileImagen=new File(Environment.getExternalStorageDirectory(),RUTA_IMAGEN);
    boolean isCreada=fileImagen.exists();
    String nombreImagen="";
    if(isCreada==false){
        isCreada=fileImagen.mkdirs();
    }

    if(isCreada==true){
        nombreImagen=(System.currentTimeMillis()/1000)+".jpg";
    }


    path=Environment.getExternalStorageDirectory()+
            File.separator+RUTA_IMAGEN+File.separator+nombreImagen;

    File imagen=new File(path);

    Intent intent=null;
    intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    ////
    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N)
    {
        String authorities=getApplicationContext().getPackageName()+".provider";
        Uri imageUri=FileProvider.getUriForFile(this,authorities,imagen);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    }else
    {
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imagen));
    }
    startActivityForResult(intent,COD_FOTO);

    ////
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode==RESULT_OK){

        switch (requestCode){
            case COD_SELECCIONA:
                Uri miPath=data.getData();
                imagen.setImageURI(miPath);
                break;

            case COD_FOTO:
                MediaScannerConnection.scanFile(this, new String[]{path}, null,
                        new MediaScannerConnection.OnScanCompletedListener() {
                            @Override
                            public void onScanCompleted(String path, Uri uri) {
                                Log.i("Ruta de almacenamiento","Path: "+path);
                            }
                        });

                Bitmap bitmap= BitmapFactory.decodeFile(path);
                imagen.setImageBitmap(bitmap);

                break;
        }


    }
}

}

    
asked by Raúl Cuc 04.01.2019 в 20:27
source

0 answers