我正在创建运行时图像视图并根据图像设置宽度和高度,它的工作完美在android棉花糖下面,但它不适用于棉花糖。
我检查我的图像不是null,位图也不是null。相同的代码工作正常,并根据我显示图像..但是当这个代码运行棉花糖然后给出错误。我也给了所有的鼓励。此行的错误 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)
答案 0 :(得分:1)
检查这个答案。
单击相机按钮时。
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);
}
}
}
我希望这对你有用。