Error loading image from gallery or camera

0

I'm having the following error while loading image gallery or smartphone camera. And I can not find where this error because this only happens in some models of smartphones.

  

java.lang.RuntimeException: Unable to resume activity   {br.com.curriculo / br.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.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.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.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();
        }

    } 
    
asked by anonymous 16.06.2016 / 20:22

1 answer

-1

Speak 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.

    
16.06.2016 / 20:56