2016-07-10 36 views
0

這個問題真的讓我很失望。java.lang.ArithmeticException:在onActivityResult中被零除

蔭試圖擴大,我已經保存在一個臨時文件(下面的代碼),但是當我這樣做,我得到這個錯誤的圖片:

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> 

在這裏,我啓動相機意圖並保存畫面到一個臨時位置也從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

imageview沒有高度 - 尚未。在onLayout被調用之前它沒有高度,直到視圖被添加到內容視圖(或其子節點)並且控件已經返回到事件循環纔會發生佈局通行證。那時它會計算它的高度。在此之前它將返回0的高度。

您需要重構代碼,以便在圖像視圖可用之前不需要寬度和高度。

0

你想縮放圖像,以適應ImageView?我認爲你可以使用picasso:

@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?

+0

我使用Inflatedview來獲取圖像的ID ImagePreview – Ale4303

+0

圖像是編輯 – Ale4303

+0

膨脹函數創建此視圖的一個新實例,並inflatedView不使用,以便沒有任何改變與使用。 – phongvan

相關問題