这个问题让我很沮丧。
我试图缩放我在temp文件中保存的图片(下面的代码),但是当我这样做时,我收到了这个错误:
java.lang.ArithmeticException:除以零 引起:java.lang.ArithmeticException:除以零
这是我的onActivityResult :(来自https://developer.android.com/training/camera/photobasics.html的代码)这里导致错误,因为image.getWidth和getHeight返回0
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
mCurrentPhotoPath = mCurrentPhotoPath.replace("file:", "");
Log.i(MYTAG, "Entered setFullImageFromFilePath method");
View inflatedView = getLayoutInflater().inflate(R.layout.edit, null);
image = (ImageView) inflatedView.findViewById(R.id.ImageviewPreview);
// Get the dimensions of the View
int targetW = image.getWidth();
int targetH = image.getHeight();
Log.i(MYTAG,"var targetW in setFullImageFromFilePath is:" +targetW);
Log.i(MYTAG,"the targetH in setFullImageFromFilePath is:" + targetH);
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
Log.i(MYTAG, "the mCurrentPhotoPath in setFullImageFromFilePath is:" + mCurrentPhotoPath);
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW= bmOptions.outWidth;
int photoH = bmOptions.outHeight;
//Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
//Decode the image file into a Bitmap sized to fill the view
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
image.setImageBitmap(bitmap) ;
Intent intent2 = new Intent(MapsActivity.this, SendScreen.class);
startActivity(intent2);
}
}
但是图像视图确实有宽度和高度
<LinearLayout
android:id="@+id/viewA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:background="@android:color/holo_purple"
android:orientation="horizontal">
<ImageView
android:contentDescription="@string/desc"
android:id="@+id/ImageviewPreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="@drawable/splash_image"
/>
</LinearLayout>
这里我启动相机Intent并将图片保存到https://developer.android.com/training/camera/photobasics.html的临时位置,可能是图片没有正确存储,因此宽度和高度为0
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
public void launchCamera(View v) {
if (ContextCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.sanchez.worldgramproject",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
答案 0 :(得分:1)
imageview还没有高度。在调用onLayout之前它没有高度,直到将视图添加到内容视图(或它的子节点)之后才会发生这种情况,并且控件已返回到事件循环,因此它可以执行布局通行证。此时它将计算其高度。在此之前它将返回0的高度。
您需要重构代码,以便在图像视图可用之前不需要它的宽度和高度。
答案 1 :(得分:0)
您希望缩放图像适合ImageView吗?我想你可以使用毕加索:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Log.i(MYTAG, "Entered setFullImageFromFilePath method");
View inflatedView = getLayoutInflater().inflate(R.layout.edit, null);
image = (ImageView) inflatedView.findViewById(R.id.ImageviewPreview);
Picasso.with(context).load(new File(mCurrentPhotoPath )).into(image);
Intent intent2 = new Intent(MapsActivity.this, SendScreen.class);
startActivity(intent2);
}
}
但是,我不知道你在哪里使用inflatedView?