Error when taking photo with android camera - Glideexception - Onloadfailed

Asked

Viewed 47 times

1

I’m trying to implement the function of taking the photo with the camera in my app, but without I’m falling into the treatment of failure in Glide onLoadFailed. I have tested other logics and can not understand why always fall into onLoadFailed.

private ImageView imageView;
private Button button_enviar;
private Uri uri_imagem = null;
public static final int PICK_IMAGE = 0;
public static final int PICK_IMAGE_CAMERA = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_storage_upload);

    imageView = (ImageView) findViewById(R.id.imagem_storage_upload);
    button_enviar = (Button) findViewById(R.id.button_enviar_storage_upload);

    button_enviar.setOnClickListener(this);

    permissao();
}

// --------------------------------------------------- TRATAMENTO DE CLICKS ---------------------------------------------------

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.button_enviar_storage_upload:
            Toast.makeText(getBaseContext(), "Enviar", Toast.LENGTH_SHORT).show();
            break;
    }
}

// --------------------------------------------------- CRIAR MENU ---------------------------------------------------

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_storage_upload, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.item_galeria:
            obterImagem_Galeria();
            return true;
        case R.id.item_camera:
            obterImagem_Camera();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

// --------------------------------------------------- OBTER IMAGEM ---------------------------------------------------

private void obterImagem_Galeria() {

    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);

    startActivityForResult(Intent.createChooser(intent, "Ecolha uma Imagem"), PICK_IMAGE);

}

// --------------------------------------------------- OBTER IMAGEM CAMERA -----------------------------------------------------------------------

private void obterImagem_Camera() {

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    File diretorio = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

    String nomeImagem = diretorio.getPath()+"/"+"CursoImagem"+System.currentTimeMillis()+".jpg";

    File file = new File(nomeImagem);

    String autorizacao = "com.example.gymplus";

    uri_imagem = FileProvider.getUriForFile(getBaseContext(), autorizacao, file);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri_imagem);

    startActivityForResult(intent, PICK_IMAGE_CAMERA);

}

// --------------------------------------------------- RESPOSTAS DE COMUNICAÇÃO  -----------------------------------------------------------------------

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode){
        case PICK_IMAGE:
            if(resultCode == RESULT_OK){
                if (data != null) {
                    uri_imagem = data.getData();
                    Glide.with(getBaseContext()).asBitmap().load(uri_imagem).listener(new RequestListener<Bitmap>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                            Toast.makeText(getBaseContext(), "Falha ao selecionar Imagem", Toast.LENGTH_SHORT).show();
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                            return false;
                        }
                    }).into(imageView);
                } else {
                    Toast.makeText(getBaseContext(), "Falha ao selecionar Imagem", Toast.LENGTH_SHORT).show();
                }
            }
            break;

        case PICK_IMAGE_CAMERA:
            if(resultCode == RESULT_OK){
                if(uri_imagem != null){
                    Glide.with(getBaseContext()).asBitmap().load(uri_imagem).listener(new RequestListener<Bitmap>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                            Toast.makeText(getBaseContext(), "Falha ao tirar Imagem", Toast.LENGTH_SHORT).show();
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                            return false;
                        }
                    }).into(imageView);
                } else {
                    Toast.makeText(getBaseContext(), "Falha ao tirar Imagem", Toast.LENGTH_SHORT).show();
                }
            }
    }
}




// ------------------------------------------------------------ PERMISSÕES USUÁRIO ----------------------------------------------------------------

private void permissao() {

    String permissoes[] = {Manifest.permission.CAMERA};

    Permissao.permissao(this, 0, permissoes);

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    for (int result : grantResults) {
        if (result == PackageManager.PERMISSION_DENIED) {
            Toast.makeText(this, "Aceite as permissões para o aplicativo acessar sua câmera", Toast.LENGTH_LONG).show();
            finish();
            break;
        }
    }
}

}

Provider.xml file

      <?xml version="1.0" encoding="utf-8"?>
        <paths xmlns:android="http://schemas.android.com/apk/res/android">
          <external-path name="external_files" path="."/>
        </paths>

Manifests

    <provider
        android:authorities="com.example.gymplus"
        android:exported="false"
        android:grantUriPermissions="true"
        android:name="android.support.v4.content.FileProvider">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider"/>
    </provider>
  • Hello, Lucca! If an error message is shown, it would be interesting edit your question and include it. Print GlideException

  • Good evening Ivan! In case the "error" that occurs is in the image display. The application succeeds in the steps of opening the camera and taking the photo, but the photo taken is not being displayed in XML Imageview. I performed some tests and concluded that after taking the photoshoot the condition that evaluates whether the image is actually returning an image (uri_image != null) is returning null and falling into Else by displaying Toast. I managed to "tidy up", but using bitmap... but this would not be my first option...

1 answer

0

The bitmap code is:

        case PICK_IMAGE_CAMERA:
            status = true;
            if(resultCode != StorageUploadActivity.RESULT_CANCELED){
                Bundle extras = data.getExtras();
                Bitmap bitmap = (Bitmap) extras.get("data");
                imageView.setImageBitmap(bitmap);
            }

This solves the problem, but not in the way I want... My goal is to take a photo and upload it with a considerable resolution..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.