gpt4 book ai didi

android - bitmap.createscaledbitmap 在棉花糖中不起作用

转载 作者:行者123 更新时间:2023-11-29 01:24:33 24 4
gpt4 key购买 nike

我正在创建运行时 ImageView 并根据图像设置宽度和高度,它在 android marshmallows 下工作完美,但在 marshmallows 中不工作。

我正在检查我的图像不为空并且位图也不为空。根据我的说法,相同的代码工作正常并显示图像。但是当运行此代码时,棉花糖出现错误。我也给予了所有许可。 erroe for this line 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com