2017-02-11 183 views
1

我一直在尝试捕获图像并调整其大小(以显示它)并存储其base64值。BitmapFactory.decodeFile在某些设备上返回null

private String setImageAndReturnBase64String(ImageView imageView) { 

    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath); 
    bitmap = Bitmap.createScaledBitmap(bitmap, 400, 400, false); 
    imageView.setImageBitmap(bitmap); 

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 40, byteArrayOutputStream); 
    byte[] byteArray = byteArrayOutputStream.toByteArray(); 

    return Base64.encodeToString(byteArray, Base64.DEFAULT); 
} 

哪里mCurrentPhotoPath

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    String imageFileName = "JPG_" + timeStamp + "_"; 
    File image = File.createTempFile(
      imageFileName, /* prefix */ 
      ".jpg",   /* suffix */ 
      storageDir  /* directory */ 
    ); 

    mCurrentPhotoPath = image.getAbsolutePath(); 

storageDir

File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); 
    if (!(storageDir != null && storageDir.exists())) { 
     if (storageDir != null) { 
      storageDir.mkdir(); 
     } 
    } 

所有必要的权限,并在清单的文件提供者。

但在某些设备BitmapFactory.decodeFile(mCurrentPhotoPath);返回null导致运行时异常。从火力地堡

堆栈跟踪:

android.app.ActivityThread.handleResumeActivity (ActivityThread.java:3152) 
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2495) 
android.app.ActivityThread.-wrap11 (ActivityThread.java) 
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1354) 
android.os.Handler.dispatchMessage (Handler.java:102) 
android.os.Looper.loop (Looper.java:148) 
android.app.ActivityThread.main (ActivityThread.java:5443) 
java.lang.reflect.Method.invoke (Method.java) 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:728) 
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:618) 
arrow_drop_down 
Caused by java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=null} to activity {com.app.in/com.app.activities.JobCard}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference 
android.app.ActivityThread.deliverResults (ActivityThread.java:3720) 
android.app.ActivityThread.performResumeActivity (ActivityThread.java:3107) 
android.app.ActivityThread.handleResumeActivity (ActivityThread.java:3152) 
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2495) 
android.app.ActivityThread.-wrap11 (ActivityThread.java) 
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1354) 
android.os.Handler.dispatchMessage (Handler.java:102) 
android.os.Looper.loop (Looper.java:148) 
android.app.ActivityThread.main (ActivityThread.java:5443) 
java.lang.reflect.Method.invoke (Method.java) 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:728) 
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:618) 
arrow_drop_down 
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference 
android.graphics.Bitmap.createScaledBitmap (Bitmap.java:636)) 
android.app.Activity.dispatchActivityResult (Activity.java:6442) 
android.app.ActivityThread.deliverResults (ActivityThread.java:3716) 
android.app.ActivityThread.performResumeActivity (ActivityThread.java:3107) 
android.app.ActivityThread.handleResumeActivity (ActivityThread.java:3152) 
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2495) 
android.app.ActivityThread.-wrap11 (ActivityThread.java) 
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1354) 
android.os.Handler.dispatchMessage (Handler.java:102) 
android.os.Looper.loop (Looper.java:148) 
android.app.ActivityThread.main (ActivityThread.java:5443) 
java.lang.reflect.Method.invoke (Method.java) 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:728) 
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:618) 

如何防止这种运行时异常情况发生在某些设备上?

编辑:在实施评论中建议的方法后,这是我一直面临的。

Exception java.lang.RuntimeException: Unable to resume activity {com.app.in/com.app.activities.Detailed}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:0, request=0, result=-1, data=null} to activity {com.app.in/com.app.activities.Detailed}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference 
android.app.ActivityThread.performResumeActivity (ActivityThread.java:3121) 
android.app.ActivityThread.handleResumeActivity (ActivityThread.java:3152) 
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2495) 
android.app.ActivityThread.-wrap11 (ActivityThread.java) 
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1354) 
android.os.Handler.dispatchMessage (Handler.java:102) 
android.os.Looper.loop (Looper.java:148) 
android.app.ActivityThread.main (ActivityThread.java:5443) 
java.lang.reflect.Method.invoke (Method.java) 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:728) 
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:618) 

Caused by java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:0, request=0, result=-1, data=null} to activity {com.app.in/com.app.activities.Detailed}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference 
android.app.ActivityThread.deliverResults (ActivityThread.java:3720) 
android.app.ActivityThread.performResumeActivity (ActivityThread.java:3107) 
android.app.ActivityThread.handleResumeActivity (ActivityThread.java:3152) 
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2495) 
android.app.ActivityThread.-wrap11 (ActivityThread.java) 
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1354) 
android.os.Handler.dispatchMessage (Handler.java:102) 
android.os.Looper.loop (Looper.java:148) 
android.app.ActivityThread.main (ActivityThread.java:5443) 
java.lang.reflect.Method.invoke (Method.java) 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:728) 
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:618) 

Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference 
com.app.fragments.ModeDetailsFragment.setImageAndReturnBase64String (ModeDetailsFragment.java:224) 
com.app.fragments.ModeDetailsFragment.onActivityResult (ModeDetailsFragment.java:198) 
android.app.Activity.dispatchActivityResult (Activity.java:6466) 
android.app.ActivityThread.deliverResults (ActivityThread.java:3716) 
android.app.ActivityThread.performResumeActivity (ActivityThread.java:3107) 
android.app.ActivityThread.handleResumeActivity (ActivityThread.java:3152) 
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2495) 
android.app.ActivityThread.-wrap11 (ActivityThread.java) 
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1354) 
android.os.Handler.dispatchMessage (Handler.java:102) 
android.os.Looper.loop (Looper.java:148) 
android.app.ActivityThread.main (ActivityThread.java:5443) 
java.lang.reflect.Method.invoke (Method.java) 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:728) 
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:618) 

改进方法

private String setImageAndReturnBase64String(ImageView imageView) { 

    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath); 
    if(bitmap == null) { 
     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(mCurrentPhotoPath, options); 
     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, 400, 400); 
     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, options); 
    } else { 
     bitmap = Bitmap.createScaledBitmap(bitmap, 400, 400, false); 
    } 
    imageView.setImageBitmap(bitmap); 
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 40, byteArrayOutputStream); 
    byte[] byteArray = byteArrayOutputStream.toByteArray(); 

    return Base64.encodeToString(byteArray, Base64.DEFAULT); 
} 

private int calculateInSampleSize(
     BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 

     final int halfHeight = height/2; 
     final int halfWidth = width/2; 

     // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
     // height and width larger than the requested height and width. 
     while ((halfHeight/inSampleSize) >= reqHeight 
       && (halfWidth/inSampleSize) >= reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 

    return inSampleSize; 
} 
+1

我的猜测是您的堆空间不足。而不是在整个图像中读取,然后缩小它,使用'BitmapFactory.Options'和'inSampleSize'来读取一个缩小的图像,并使用较少的内存。 – CommonsWare

+0

VM免费:7.21MB VM总:19.58MB VM最大:96MB 电池电量:71% 制造商:LG电子 型号:LG-K420 局:Msm8916 Android的API:22 Android操作系统5.1.1 品牌:Lge 内存:1.86GB 这是崩溃期间的设备数据。 –

+0

@CommonsWare你可以看看这个问题吗? –

回答

0

BitmapFactory.decodeFile()将返回null如果Bitmap会变得太大可用内存。你可以每周在这个网站上阅读这个'功能'。

只需尝试一个较小的图像。

相关问题