Error loading gallery or camera image

Asked

Viewed 213 times

0

I’m having the following error while loading image gallery or smartphone camera. And I can’t find where this error because it only happens on some smartphone models.

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.Ismiss()' 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)

My code below:

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();
        }

    } 
  • "'void android.app.Progressdialog.Dismiss()' on a null Object Reference at " try to check if your 'pDialog' is null before using the Dismiss method

  • pDialog = new Progressdialog(Perfil_activity.this); pDialog.Dismiss(); this giving error why this pDialog line is null, the pDialog variable is declared in the general body of Activity???

1 answer

-1

This is Osvaldo, try to use this code below, for me it works on all 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;
    }

Hugs.

  • 3

    You are not helping to clear the doubt that the guy had, he wants to know how to solve his problem some cell phones do not work, probably your answer may work but does not take the doubt

Browser other questions tagged

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