2017-01-09 86 views
0

我從圖庫加載圖像和裁剪使用Android Image Cropper令人驚訝的例外:嘗試調用虛擬方法「java.lang.String中android.net.Uri.getScheme()」上的空對象引用

onActivityResult方法代碼

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == Activity.RESULT_OK) { 

      if (requestCode == CHOOSE_IMAGE) { 

       CropImage.activity (data.getData ()) 
         .setGuidelines (CropImageView.Guidelines.ON) 
         .start (this); 

      } else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) { 
       Uri imageUri = getPickImageResultUri (data); 

       // For API >= 23 we need to check specifically that we have permissions to read external storage, 
       // but we don't know if we need to for the URI so the simplest is to try open the stream and see if we get error. 
       boolean requirePermissions = false; 
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && 
         checkSelfPermission (Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED && 
         isUriRequiresPermissions (imageUri)) { 

        // request permissions and handle the result in onRequestPermissionsResult() 
        requirePermissions = true; 
        mCropImageUri = imageUri; 
        requestPermissions (new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0); 
       } 


       if (!requirePermissions) { 

       } 

     mCropImageUri = imageUri; 
       try { 
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver() , mCropImageUri); 

       mCropImageView.setImageBitmap (bitmap); 

       } catch (IOException e) { 
        e.printStackTrace (); 
       } 
//    mImageLoader.displayImage (String.valueOf (mCropImageUri),mUiCircularImage,options); 

      } 
     } 
    } 

一切都很好。

resultCode沒問題。

requestCode是罰款

Data不爲空

ImageUri

file:///storage/emulated/0/Android/data/com.hey.heypickup/cache/pickImageResult.jpeg 

,但是當我想轉換上面的URI成位圖使用下面的代碼

Bitmap bitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver() , mCropImageUri) 

獲取異常

FATAL EXCEPTION: main 
        Process: com.hey.heypickup, PID: 21994 
        java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=203, result=-1, data=Intent { (has extras) }} to activity {com.hey.heypickup/com.hey.heypickup.Activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference 
         at android.app.ActivityThread.deliverResults(ActivityThread.java:3790) 
         at android.app.ActivityThread.handleSendResult(ActivityThread.java:3833) 
         at android.app.ActivityThread.access$1700(ActivityThread.java:152) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1390) 
         at android.os.Handler.dispatchMessage(Handler.java:102) 
         at android.os.Looper.loop(Looper.java:135) 
         at android.app.ActivityThread.main(ActivityThread.java:5538) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at java.lang.reflect.Method.invoke(Method.java:372) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference 
         at android.content.ContentResolver.openInputStream(ContentResolver.java:641) 
         at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:849) 
         at com.hey.heypickup.Activities.MainActivity.onActivityResult(MainActivity.java:128) 
         at android.app.Activity.dispatchActivityResult(Activity.java:6238) 
         at android.app.ActivityThread.deliverResults(ActivityThread.java:3786) 
         at android.app.ActivityThread.handleSendResult(ActivityThread.java:3833)  
         at android.app.ActivityThread.access$1700(ActivityThread.java:152)  
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1390)  
         at android.os.Handler.dispatchMessage(Handler.java:102)  
         at android.os.Looper.loop(Looper.java:135)  
         at android.app.ActivityThread.main(ActivityThread.java:5538)  
         at java.lang.reflect.Method.invoke(Native Method)  
         at java.lang.reflect.Method.invoke(Method.java:372)  
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)  
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)  

這樣完蛋了......

+0

的可能的複製[安卓應用程序上onActivityResult崩潰,當使用相機意向書(HTTP ://stackoverflow.com/questions/34504717/android-app-crashes-on-onactivityresult-while-using-camera-intent) –

+0

@juan克魯茲在我的情況下沒有任何東西是空的,我從圖片庫 –

+0

你可以更新getPickImageResultUri()和isUriRequiresPermissions()方法的代碼? – rogerwar

回答

1

mCropImageUrinull

特別是,如果這個測試是false,你不必在你的問題的代碼,它分配一個值:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && 
        checkSelfPermission (Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED && 
        isUriRequiresPermissions (imageUri)) 
相關問題