2016-12-19 28 views
2

我裁剪圖像與活動結果時有問題。我已在設備上設置權限> 6。但是當播種和作物成功時,結果數據爲空。 (imageReturnedIntent.getData()我們null和intent數據也是空的)。這是代碼裁剪圖像。Android作物行動getData null與某些設備

public static Intent cropIntent(Uri inUri, int outputX, int outputY, 
            boolean isScale) { 
     Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
     cropIntent.setDataAndType(inUri, "image/*"); 
     cropIntent.putExtra("crop", "true"); 
     cropIntent.putExtra("aspectX", outputX > 0 ? outputX : 100); 
     cropIntent.putExtra("aspectY", outputY > 0 ? outputY : 100); 
     cropIntent.putExtra("outputX", outputX > 0 ? outputX : 100); 
     cropIntent.putExtra("outputY", outputY > 0 ? outputY : 100); 
     cropIntent.putExtra("scale", isScale); 
     File file = new File(Environment.getExternalStorageDirectory() + File.separator + "img.jpg"); 
     Uri mCropImagedUri = Uri.fromFile(file); 
     cropIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mCropImagedUri); 
     cropIntent.putExtra("return-data", true); 
     return cropIntent; 
    } 

這是代碼activitysult

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
     super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
     if (callbackManager != null) { 
      callbackManager.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

     } 

     switch (requestCode) { 
      case SELECT_PHOTO: 
       if (resultCode == RESULT_OK) { 

        Uri uriImage = imageReturnedIntent.getData(); 
        System.out.println(">>>> uri "+uriImage); 
       // CommonUtils.cropImage(uriImage,HomeActivity.this,SELECT_CROP_IMAGE); 
        startActivityForResult(CommonUtils.cropIntent(uriImage, 
          200, 200, true), SELECT_CROP_IMAGE); 
       } 
       break; 
      case SELECT_CROP_IMAGE: 
       if (resultCode == RESULT_OK) { 
       // Uri uri = imageReturnedIntent.getData(); 

        System.out.println(">>>>> "+imageReturnedIntent.getData()); 
        Bundle extras = imageReturnedIntent.getExtras(); 
        if (extras == null) { 
         System.out.println(">>>>> check intent"); 
         return; 
        } 
        // Bitmap bitMapScale = extras.getParcelable("data"); 
        Uri uri = extras.getParcelable(MediaStore.EXTRA_OUTPUT); 
        try { 
         Bitmap bitMapScale = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri); 
         System.out.println(">>>>>> check " + bitMapScale); 
         //  yourSelectedImage.recycle(); 
         // base64 
         String myBase64Image = CommonUtils.encodeToBase64(bitMapScale, Bitmap.CompressFormat.PNG, 100); 
         // bitMapScale.recycle(); 
         assert uri != null; 
         LocalStorage.getShareInstance().saveUriAvatar(uri.toString()); 
         LocalStorage.getShareInstance().saveBase64Image(myBase64Image); 
         UserModel userModel = LocalStorage.getShareInstance().getUserLogin(); 
         if (userModel == null) { 
          // if user have not login. 
          if (uiLeftMenu != null) { 
           uiLeftMenu.fillImage(uri); 
          } 
         } else { 
          System.out.println(">>>>>> check data"); 
          if (CommonUtils.isStringDataValid(myBase64Image)) { 

           upLoadImageToServerWithStringBase64(myBase64Image); 
          } 
         } 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 


       } 
       break; 
     } 
    } 

問題是什麼?

+1

有時默認裁切不起作用。你可以使用這個庫https://github.com/biokys/cropimage/tree/master/simple-crop-image-lib –

+1

com.android.camera.action.CROP沒有正式的支持,可以不要工作。所以沒有解決辦法。事件並非所有設備都支持它。 –

回答

0

我也在Huwai設備上遇到同樣的問題。某些設備不通過intent.getData()發送目的地Uri。但經過一些調試後,我發現uri設置爲mAction字符串變量,可以通過intent.getAction()方法訪問。然後你可以用'Uri.parse(outPutFilePath)'這個方法生成Uri。您可以先檢查intent.getData()是否爲空。如果爲空,則使用intent.getAction()方法將Uri作爲字符串並生成輸出Uri。 我以這種方式解決了我的問題。希望這會有所幫助。

相關問題