0

Estou tendo o seguinte erro ao carregar galeria de imagem ou câmera smartphone. E eu não consigo encontrar onde este erro porque isso só acontece em alguns modelos de smartphones.

java.lang.RuntimeException: Unable to resume activity {br.com.curriculo/br.com.curriculo.Perfil_Activity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=4000, result=-1, data=Intent { dat=content://media/external/images/media/5636 flg=0x1 }} to activity {br.com.curriculo/br.com.curriculo.Perfil_Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ProgressDialog.dismiss()' on a null object reference at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3121) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3152) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5443) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=4000, result=-1, data=Intent { dat=content://media/external/images/media/5636 flg=0x1 }} to activity {br.com.curriculo/br.com.curriculo.Perfil_Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ProgressDialog.dismiss()' on a null object reference at android.app.ActivityThread.deliverResults(ActivityThread.java:3720) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3107) ... 10 more Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ProgressDialog.dismiss()' on a null object reference at br.com.curriculo.mecontrate.Perfil_Activity.onActivityResult(Perfil_Activity.java:589) at android.app.Activity.dispatchActivityResult(Activity.java:6442) at android.app.ActivityThread.deliverResults(ActivityThread.java:3716)

Meu código abaixo:

public  void opengalleryProfile(){
        pDialog = new ProgressDialog(Perfil_Activity.this);
        pDialog.setMessage(Perfil_Activity.this.getString(R.string.aguarde));
        pDialog.setIndeterminate(true);
        pDialog.setCancelable(true);
        pDialog.show();
        Intent i = new Intent(
                Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, GALERIA_PERFIL);
    }

public void openCamera(){
        NomeArquivo = DateFormat.format(
                "dd-MM-yyyy hh_mm_ss", new Date()).toString();
File picsDir = Environment.getExternalStoragePublicDirectory(getString(R.string.app_name) + File.separator + getString(R.string.app_name)+" "+ getString(R.string.pasta_imagens) );
        imageFile = new File(picsDir,NomeArquivo +".jpg");
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
        startActivityForResult(intent, CAMERA_PERFIL);
    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == GALERIA_PERFIL){
            pDialog.dismiss();
        if (resultCode == RESULT_OK && data != null) {
            try {
                mImageCaptureUri = data.getData();
                performCropPerfil(mImageCaptureUri);
            } catch (Exception e) {
                Toast.makeText(this, R.string.falhaImagem,
                        Toast.LENGTH_SHORT).show();
            }
        }
        }

        if (requestCode == CAMERA_PERFIL){
         if ( resultCode == RESULT_OK && imageFile != null) {
             try {
                 performCropPerfil(Uri.fromFile(imageFile));

             } catch (Exception e) {
                 Toast.makeText(this, R.string.falhaImagem,
                        Toast.LENGTH_SHORT).show();
             }

         }
        }

        if (requestCode == CROP_FROM_CAMERA) {
            if ( resultCode == RESULT_OK && data != null) {

                Bundle extras = data.getExtras();

                if (extras != null) {
                    try {


                        bmpArquivo = extras.getParcelable("data");

                        imgPerfil.setImageBitmap(bmpArquivo);

                    } catch (Exception e) {
                        Toast.makeText(this, R.string.falhaImagem,
                                Toast.LENGTH_SHORT).show();

                    }
                }

            }
        }
    }

    private void performCropPerfil(Uri uri) {

        try {

            Intent cropIntent = new Intent("com.android.camera.action.CROP");
            cropIntent.setDataAndType(uri, "image/*");
            cropIntent.putExtra("scale", true);
            cropIntent.putExtra("aspectX", 1);
            cropIntent.putExtra("aspectY", 1);
            cropIntent.putExtra("outputX",128);
            cropIntent.putExtra("outputY", 128);
            cropIntent.putExtra("return-data", true);
            startActivityForResult(cropIntent, CROP_FROM_CAMERA);
        }

        catch (ActivityNotFoundException anfe) {
            Toast toast = Toast
                    .makeText(this, R.string.falhaCortarImagem, Toast.LENGTH_SHORT);
            toast.show();
        }

    } 
2
  • "'void android.app.ProgressDialog.dismiss()' on a null object reference at " tente verificar se está null o seu 'pDialog' antes de usar o método dismiss 16/06/2016 às 20:00
  • pDialog = new ProgressDialog(Perfil_Activity.this); pDialog.dismiss(); esta dando erro por que nessa linha pDialog esta nulo, a variavel pDialog esta declarada no corpo geral da activity??? 25/06/2018 às 12:54

1 Resposta 1

-1

Fala Osvaldo, tenta usar esse código abaixo, pra mim funciona em todos smartphones:

private String selectedImagePath = "";
final private int PICK_IMAGE = 1;
final private int CAPTURE_IMAGE = 2;

public Uri setImageUri() {
        // Store image in dcim
        File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png");
        Uri imgUri = Uri.fromFile(file);
        this.imgPath = file.getAbsolutePath();
        return imgUri;
    }


    public String getImagePath() {
        return imgPath;
    }

btnGallery.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);

            }
        });

        btnCapture.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
                startActivityForResult(intent, CAPTURE_IMAGE);
            }
        });

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_CANCELED) {
            if (requestCode == PICK_IMAGE) {
                selectedImagePath = getAbsolutePath(data.getData());
                imgUser.setImageBitmap(decodeFile(selectedImagePath));
            } else if (requestCode == CAPTURE_IMAGE) {
                selectedImagePath = getImagePath();
                imgUser.setImageBitmap(decodeFile(selectedImagePath));
            } else {
                super.onActivityResult(requestCode, resultCode, data);
            }
        }

    }


public Bitmap decodeFile(String path) {
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, o);
            // The new size we want to scale to
            final int REQUIRED_SIZE = 70;

            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeFile(path, o2);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;

    }

public String getAbsolutePath(Uri uri) {
        String[] projection = { MediaColumns.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }

Abraços.

1
  • 3
    Você não está ajudando a tirar a dúvida que o cara teve, ele quer saber como resolver o problema dele de alguns celulares não funcionar, provavelmente sua resposta pode funcionar mas nao tira a dúvida 16/06/2016 às 19:57

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .