我正在创建运行时图像视图,并根据图像设置宽度和高度,它在android棉花糖下的工作完美,但在棉花糖中不起作用。

我检查我的图像不为空,位图也不为空。相同的代码工作正常,并根据我显示图像..但是,当运行此代码时,棉花糖会给出错误。我也给了所有许可。这行的错误image.getMeasuredWidth()这行被给予错误

这是我的代码-

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


该代码给出了错误:

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)

最佳答案

检查这个答案。

单击相机按钮时。

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


并添加这个

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


希望这对您有用。

关于android - bitmap.createscaledbitmap在棉花糖中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34782716/

10-10 09:33