2015-08-08 39 views
-2

我正在開發一個Android應用程序,該應用程序允許用戶裁剪位於我的Drawable文件夾中的圖像。這是我正在使用的代碼,但它顯示錯誤。我怎樣才能解決這個問題?如何在Android中裁剪drawable?

@Override 
public void onClick(View v) { 

    Uri imgUri=Uri.parse("android.resource://com.example.cropapp/"+R.drawable.apples); 
    Intent intent = new Intent("com.android.camera.action.CROP"); 
      intent.setDataAndType(imgUri, "image/*"); 
      intent.putExtra("crop", "true"); 
      intent.putExtra("aspectX", 1); 
      intent.putExtra("aspectY", 1); 
      intent.putExtra("outputX", 80); 
      intent.putExtra("outputY", 80); 
      intent.putExtra("return-data", true); 
      startActivityForResult(intent, 1); 
    } 
+0

請粘貼錯誤。 – EliaszKubala

回答

4

我該如何解決這個問題?

首先認識到Android does not have a CROPIntent。有很多image cropping libraries for Android。使用一個。其次,認識到很少有應用程序宣傳<intent-filter>結構支持使用不當的android.resource方案。切換到圖像裁剪庫時,這不會成爲問題,因爲一切都將在您自己的應用程序中。

但是,請記住,大多數圖像裁剪方案涉及的圖像是文件,而不是資源,更不用說可繪製資源。你是人類歷史上第一個想要讓用戶收穫可繪製資源的人是完全有可能的,所以你很可能不得不自己開發一下。

+0

我不能完全同意你,檢查這個接受的答案http://stackoverflow.com/a/15239086/2506025 –

+0

@ProkashSarkar:隨意提供一個鏈接到'CROP'' Intent'記錄在Android SDK。如果Google要求設備製造商提供「CROP」「Intent」以便在其設備上擁有Play商店和其他Google專有應用,請隨時提供鏈接。 – CommonsWare

+0

我被很多人認爲使用這種方法工作正常的帖子感到困惑。經過一番搜索,我明白了這一點,它實際上得到了支持,但不是正式的。感謝您找出問題,我已經更新了我的答案。 –

0

下面的代碼片段是對於像AOSP相機應用程序自定義光盤,而不是正式提供或支持大部分設備

可能的解決方案?

嘗試這樣一個非官方庫,

https://github.com/lvillani/android-cropimage

代碼示例:

@Override 
    public void onClick(View view) { 
     if (view.equals(button)) { 
      startActivityForResult(MediaStoreUtils.getPickImageIntent(this), REQUEST_PICTURE); 
     } 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     File croppedImageFile = new File(getFilesDir(), "test.jpg"); 

     if ((requestCode == REQUEST_PICTURE) && (resultCode == RESULT_OK)) { 
      // When the user is done picking a picture, let's start the CropImage Activity, 
      // setting the output image file and size to 200x200 pixels square. 
      Uri croppedImage = Uri.fromFile(croppedImageFile); 

      CropImageIntentBuilder cropImage = new CropImageIntentBuilder(200, 200, croppedImage); 
      cropImage.setOutlineColor(0xFF03A9F4); 
      cropImage.setSourceImage(data.getData()); 

      startActivityForResult(cropImage.getIntent(this), REQUEST_CROP_PICTURE); 
     } else if ((requestCode == REQUEST_CROP_PICTURE) && (resultCode == RESULT_OK)) { 
      // When we are done cropping, display it in the ImageView. 
      imageView.setImageBitmap(BitmapFactory.decodeFile(croppedImageFile.getAbsolutePath())); 
     } 
    } 
+0

」Android聯繫人管理器EditContactActivity使用Intent(「com.android.camera.action.CROP」),它可以用來裁剪圖像「 - 不,它不。首先,再也沒有這樣的活動了,正如你可以通過查看[清單](https://android.googlesource.com/platform/packages/apps/Contacts/+/master/AndroidManifest.xml)所說的那樣。其次,[聯繫編輯器活動](https://android.googlesource.com/platform/packages/apps/Contacts/+/master/src/com/android/contacts/activities/ContactEditorActivity.java)不使用'作物''意圖'。 – CommonsWare

+0

即使您返回版本歷史記錄中的某種方式,但在AOSP通訊錄應用程序具有「EditContactActivity」時,它仍不會使用「CROP」「Intent」,至少不是所有版本。例如,請參閱[此版本](https://android.googlesource.com/platform/packages/apps/Contacts/+/34aeeb0f956357fedbe16fbb0fd1172c9bdfaa1a/src/com/android/contacts/ui/EditContactActivity.java)。很可能,曾經有一次'EditContactActivity'使用了這種'Intent',但現在已經不存在了。 – CommonsWare

+0

感謝您解決問題。事實上,它很榮幸能夠糾正你的錯誤。希望任何人通過這篇文章不會再嘗試,因爲它在許多文章/教程中被錯誤地描述。 –