2013-01-21 86 views
3

我有一個應用程序崩潰,我似乎無法解決。創建位圖NullPointerException

我從像這樣的資源獲取位圖。

Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.map_distance_tag); 

現在,這似乎有問題的設備,當涉及到從該位圖創建一個位圖的極少數的少數。例如...

Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig()); 

我得到了NullPointerException時,它創建於Bitmap類468線Bitmap,釋我不得不走進一看其當其從設置新位圖的密度及其規模源位圖。

// The new bitmap was created from a known bitmap source so assume that 
467  // they use the same density scale 
468  bitmap.setDensityScale(source.getDensityScale()); 

現在我似乎無法重現的問題,但事實上,當的getWidth()調用原來的位圖告訴我,源不是空我沒有得到一個例外。

這是整個堆棧跟蹤。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.app/com.my.app.recording.TrackingActivity}: java.lang.NullPointerException 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:123) 
at android.app.ActivityThread.main(ActivityThread.java:4627) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:521) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException 
at android.graphics.Bitmap.createBitmap(Bitmap.java:468) 
at com.my.app.maps.MapFragment.createTabOverlay(MapFragment.java:663) 
at com.my.app.maps.MapFragment.addMarkersForToolType(MapFragment.java:474) 
at com.my.app.maps.MapFragment.onActivityCreated(MapFragment.java:405) 
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1468) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) 
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444) 
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551) 
at com.my.app.recording.TrackingActivity.onStart(TrackingActivity.java:272) 
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129) 
at android.app.Activity.performStart(Activity.java:3781) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636) 
... 11 more 

我的想法

這可能是一些與我不包括針對不同屏幕密度不同的位圖?所有提供的位圖均用於mdpi屏幕。

我注意到還有一個BackStackRecord。這會在用戶按下時崩潰嗎?

預先感謝

回答

4

Bitmap.java:468爲你點源Android 1.5,可能不是你的問題。

v1.5 Bitmap.java:468

bitmap.setDensityScale(source.getDensityScale()); 

v1.6-v2.2 Bitmap.java:468

Bitmap bm = nativeCreate(null, 0, width, width, height, config.nativeInt, true); 

468線在任何超過2.2更新是一個javadoc塊或不相關的功能的任一部分,所以我猜你看1.6-2。2版本,而不是您嘗試排除故障的那個版本。

這條線上我唯一能看到真正結束爲空的是config。在你的代碼中,你使用getConfig()來抓取當前位圖對象。該docs它顯示:

公衆最終Bitmap.Config getConfig()

在API級別1

如果位圖的內部配置是在公共格式之一,返回的配置,否則返回null。

所以,它可能不會找到一個「公共」格式,並返回null,它傳遞給createBitmap(),並炸燬你的代碼。

您可以一直使用static config,比如Bitmap.Config.ARGB_8888,或者在前一行上執行getConfig()並對其進行空檢查。如果爲空,則轉到剛剛提到的後備配置。

1

yourSelectedImageBitmap = BitmapFactory.decodeFile(文件路徑,選項);

if (yourSelectedImageBitmap != null) { 
     // ----------resize for set in imageView------------ 
     int width = yourSelectedImageBitmap.getWidth(); 
     int height = yourSelectedImageBitmap.getHeight(); 
     int newWidth = 140; 
     int newHeight = 140; 

     // calculate the scale - in this case = 0.4f 
     float scaleWidth = ((float) newWidth)/width; 
     float scaleHeight = ((float) newHeight)/height; 

     Matrix matrix = new Matrix(); 
     // resize the bit map 
     matrix.postScale(scaleWidth, scaleHeight); 
     if (exifOrientation.equals("6")) { 
      matrix.postRotate(90); 
     } 

     Bitmap resizedBitmap = Bitmap.createBitmap(yourSelectedImageBitmap, 
       0, 0, width, height, matrix, true); 
     image.setImageBitmap(resizedBitmap); 

     // Toast.makeText(getApplicationContext(), 
     // "in the select image loop", 2000).show(); 

     // ----------resize for upload------------ 
     int width1 = yourSelectedImageBitmap.getWidth(); 
     int height1 = yourSelectedImageBitmap.getHeight(); 

您可以通過這種方式嘗試。從文件源或相機獲取圖像後。

然後通過這樣做。

Matrix matrix1 = new Matrix(); 
     // resize the bit map 
     matrix.postScale(width1, height1); 
     if (exifOrientation.equals("6")) { 
      matrix1.postRotate(90); 
     } 
     Bitmap resizedBitmap1 = Bitmap.createBitmap(
       yourSelectedImageBitmap, 0, 0, width1, height1, matrix1, 
       true); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     resizedBitmap1.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
     byte[] image_bytes = baos.toByteArray(); 

     try { 
      baos.flush(); 
      baos.close(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
      Log.e("~~~~~~~~IOException~~~~~~~~~eeeeee~~~~", 
        "~~~~~~IOException~~~~~~~~~eeeee~~~~" + e.getMessage()); 
     } 
     image_string = Base64.encodeToString(image_bytes, 0); 

希望這將幫助你

+1

對不起解決這個問題,但我從一個資源得到我的位圖 – StuStirling

1

你可以這樣做:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon); 
Bitmap bmpCompressed = Bitmap.createScaledBitmap(bitmap, 600, 400, true); 

ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

// CompressFormat set up to JPG, you can change to PNG 
bmpCompressed.compress(CompressFormat.JPEG, 90, bos); 
imageByteArray = bos.toByteArray(); 
0

我認爲這會工作

BitmapFactory.Options opt = new BitmapFactory.Options(); 
opt.inMutable = true; //you have to set bitmap to mutable 
bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.map_distance_tag,opt); 
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), Bitmap.Config.ARGB_8888); 

//if you want to copy the bmp1 to bmOverlay 
bmOverlay=Bitmap.createScaledBitmap(bmp1, bmp1.getWidth, bmp1.getHeight, false); 

     OR 

//if did not work replace bmOverlay to this: 
bmOverlay = Bitmap.createBitmap(bmp1); 
+0

不幸的是,'opt.inMutable'調用需要API級別11,我支持11級你能看出爲什麼這會崩潰嗎? – StuStirling

+0

如果你支持11級,這不應該崩潰。 –

+0

我編輯了bmOverlay變量部分,而不是「真」,我用「假」值替換它。 –

0

我有同樣的問題,這讓我瘋狂。我最終改變

Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig()); 

Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), Bitmap.Config.ARGB_8888);