2016-07-10 1 views
0

この問題は私を本当にイライラさせます。java.lang.ArithmeticException:onActivityResultの0で割る

Iamはiが一時ファイルに保存した画像(以下コード)を拡大しようとしているが、私はそうするとき、私はこのエラーを取得:

java.lang.ArithmeticExceptionます。java:によって引き起こさゼロ 除算.lang.ArithmeticException:ゼロ

除算はこれが私のonActivityResultである:(https://developer.android.com/training/camera/photobasics.htmlからコード)エラーがimage.getWidthので、ここで引き起こされ、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); 
    } 
} 

を返すのgetHeightが、画像ビューは持っていません幅と高さ。ここ

<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からも一時の場所に画像を保存するため、幅と高さ

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があります

答えて

0

画像ビューにまだ高さがありません。 onLayoutが呼び出されるまで高さがありません。これは、ビューがコンテンツビュー(またはその子)に追加され、コントロールがイベントループに戻ってそれができるようになるまで発生しませんレイアウトパス。その時点で、その高さが計算されます。それまでは0の高さが返されます。

イメージビューの幅と高さが利用可能になるまで、コードをリファクタリングする必要があります。

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をどこで使っていますか?

+0

私はInflatedviewを使用してIDで画像を取得します。ImagePreview – Ale4303

+0

画像が編集中 – Ale4303

+0

inflate関数はこのビューの新しいインスタンスを作成し、inflatedViewは使用すると変更することはありません。 – phongvan

関連する問題