捕获并显示图像问题

时间:2018-05-03 08:31:45

标签: android

我正在尝试制作一个简单的应用程序,它使用系统相机应用程序通过传递相机意图拍摄照片,将图像存储在文件系统中,然后拍摄图像并将其转换为位图并通过执行某些操作将其显示在图像视图中缩小它。我在Android版本6中测试了我的应用程序,我收到以下错误:

java.lang.RuntimeException: 
         at android.app.ActivityThread.performResumeActivity (ActivityThread.java:3273)
         at android.app.ActivityThread.handleResumeActivity (ActivityThread.java:3304)
         at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2618)
         at android.app.ActivityThread.-wrap11 (ActivityThread.java)
         at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1469)
         at android.os.Handler.dispatchMessage (Handler.java:111)
         at android.os.Looper.loop (Looper.java:207)
         at android.app.ActivityThread.main (ActivityThread.java:5692)
         at java.lang.reflect.Method.invoke (Native Method)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:905)
         at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:766)
         Caused by: java.lang.RuntimeException: 
         at android.app.ActivityThread.deliverResults (ActivityThread.java:3888)
         at android.app.ActivityThread.performResumeActivity (ActivityThread.java:3255)
         Caused by: java.lang.ArithmeticException: 
         at iforest.photogps.MainActivity.previewCapturedImage (MainActivity.java:241)
         at iforest.photogps.MainActivity.onActivityResult (MainActivity.java:147)
         at android.app.Activity.dispatchActivityResult (Activity.java:6461)
         at android.app.ActivityThread.deliverResults (ActivityThread.java:3884)

应用程序获取scaleFactor的previewCapturedImage内第241行的ArithmeticException:

/* preview the image on app's screen*/
private Bitmap previewCapturedImage() {

    // Get the dimensions of the View
    int targetW = imageView.getWidth();
    int targetH = imageView.getHeight();

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    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);  <-- line 241 

    // 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);
    //imageView.setImageBitmap(bitmap);
    return BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

}

报告没有提供足够的有关算术异常的信息。可能问题是targetW或targetH是0?

1 个答案:

答案 0 :(得分:0)

是的,当你除以零时,可以抛出ArithmeticException。设置断点或记录targetHtargetW的值。这可能是因为您在测量之前得到了imageView的高度和宽度。您可以尝试移动这些行:

int targetW = imageView.getWidth();
int targetH = imageView.getHeight();

或整个方法调用onWindowFocusChanged。或者,如果所有其他方法都失败了,this answer通过扩展ImageView解决了这个问题。