Abhinav Singh
Abhinav Singh

Reputation: 43

bitmap.createscaledbitmap is not work in marshmallows

I am creating run time image view and set width and height according to image, Its work perfect below the android marshmallows but its not working in marshmallows.

I am check my image is not null and bitmap is also not null. same code is working fine and show image according to me.. but when this code is run marshmallows then given error. I have also given all permition. erroe for this line image.getMeasuredWidth() this line is given error

this is my Code -

image.setImageBitmap(Bitmap.createScaledBitmap(bitmap, image.getMeasuredWidth(), image.getMeasuredWidth(), false));

this code has given error:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
            at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:610)

Upvotes: 2

Views: 1209

Answers (1)

Abhinav singh
Abhinav singh

Reputation: 1456

check this answer.

when click the camera button.

public void getPhotoFromCamera() {

    if (!marshMallowPermission.checkPermissionForCamera()) {
        marshMallowPermission.requestPermissionForCamera();
        marshMallowPermission.requestPermissionForExternalStorage();
       Toast.makeText(this,"camera permition",Toast.LENGTH_SHORT).show();
    } else {
        {
            Toast.makeText(this,"1111111111",Toast.LENGTH_SHORT).show();
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File mediaStorageDir = new File(
                    Environment.getExternalStorageDirectory()
                            + File.separator
                            + "IMG_"
                            + File.separator
                            + ".jpg"
            );

            if (!mediaStorageDir.exists()) {
                mediaStorageDir.mkdirs();
            }

            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                    Locale.getDefault()).format(new Date());
            try {
               File mediaFile = File.createTempFile(
                        "IMG_" + timeStamp,  /* prefix */
                        ".jpg",         /* suffix */
                        mediaStorageDir      /* directory */
                );
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mediaFile));
                startActivityForResult(takePictureIntent, 100);

                Log.e("STRING",mediaFile.getPath()+"    String   "+mediaFile);

                fileUri=Uri.fromFile(mediaFile);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

and add this also

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e("String", resultCode + "   " + requestCode);


        if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                previewCapturedImage();
            } else if (resultCode == RESULT_CANCELED) {
                Toast.makeText(getApplicationContext(),
                        "User cancelled image capture", Toast.LENGTH_SHORT)
                        .show();
            } else {
                Toast.makeText(getApplicationContext(),
                        "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                        .show();
            }
        }

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

            Log.e("Camera111", "2222222222" );
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            picturePath = cursor.getString(columnIndex);
            cursor.close();
            camera = BitmapFactory.decodeFile(picturePath);
            Log.e("Camera111", "" + camera);
            setimage(camera);
        }
    }

    private void setimage(Bitmap bitmap) {

        if (text == null) {
            ((ImageView) findViewById(R.id.restro_image)).setImageBitmap(bitmap);
        } else {
            text.setMaxHeight(text.getMeasuredWidth());
            if (picturePath != null) {

                text.setImageBitmap(Bitmap.createScaledBitmap(bitmap, text.getMeasuredWidth(), text.getMeasuredWidth(), false));
            } else {
                text.setImageBitmap(Bitmap.createScaledBitmap(bitmap, text.getMeasuredWidth(), text.getMeasuredWidth(), false));
//                text.setImageBitmap(bitmap);
            }
        }
    }

I hope this is work for you.

Upvotes: 1

Related Questions