2016-07-10 5 views
0

Dieses Problem frustriert mich wirklich.java.lang.ArithmeticException: dividiere durch Null in onActivityResult

Iam versuchen, ein Bild zu skalieren, die ich in einer temporären Datei (Code unten) gespeichert haben, aber wenn ich so bekomme ich diesen Fehler:

java.lang.ArithmeticException: Division durch Null Verursacht durch: java .lang.ArithmeticException: Division durch Null

das ist mein onActivityResult: (Code aus https://developer.android.com/training/camera/photobasics.html) der Fehler, weil der image.getWidth hier verursacht wird und das Rück 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); 
    } 
} 

Aber die Bildansicht hat eine Breite und eine Höhe

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

Hier beginne ich mit der Kamera Intent und das Bild zu einem temporären Speicherort speichern auch von https://developer.android.com/training/camera/photobasics.html kann es das Bild ist nicht korrekt gespeichert sein, dass und hat daher 0 als Breite und Höhe

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); 

       } 
      } 

Antwort

0

Die Bildansicht hat noch keine Höhe. Es hat keine Höhe, bis onLayout aufgerufen wurde, was nicht passieren wird, bis die Ansicht der Inhaltsansicht hinzugefügt wurde (oder ein untergeordnetes Element) und die Steuerung in die Ereignisschleife zurückgekehrt ist, so dass dies möglich ist ein Layout-Durchlauf. An diesem Punkt wird er seine Höhe berechnen. Bis dahin wird eine Höhe von 0 zurückgegeben.

Sie müssen Ihren Code so umgestalten, dass Sie die Breite und Höhe der Bildansicht nicht benötigen, bis sie verfügbar ist.

0

Soll das Skalierungsbild an ImageView angepasst werden? Ich denke, Sie können Picasso mit verwenden:

@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); 
    } 
} 

Allerdings weiß ich nicht, wo Sie aufgeblaseneView verwenden?

+0

Ich benutze Inflatatedview, um das Bild durch die ID ImagePreview – Ale4303

+0

das Bild ist im Inneren bearbeiten – Ale4303

+0

Funktion zu erstellen eine neue Instanz dieser Ansicht erstellen, und inflatedView nicht verwenden, so dass nichts mit der Verwendung ändern. – phongvan

Verwandte Themen