2017-06-12 183 views
0

每當我使用camera裁剪圖像時,出現錯誤Unable to load image。但在gallery的情況下,它運行良好。在android中裁剪圖像

Uri uriPath = StoreAndFetchImageFromFile.getInstance(ParentDetails.this).getImageUri(partFilename); 
       selectedimagepath = getPath(uriPath); 

       Bitmap myBitmap = BitmapFactory.decodeFile(selectedimagepath); 
       parentimage.setImageBitmap(myBitmap); 
       performCropCamera(uriPath); 

和方法imagecrop是:

private void performCropCamera(Uri picUri) { 
    // take care of exceptions 
    try { 
     // call the standard crop action intent (the user device may not 
     // support it) 
     Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
     // indicate image type and Uri 
     cropIntent.setDataAndType(picUri, "image/*"); 
     // set crop properties 
     cropIntent.putExtra("crop", "true"); 
     int asp = (int) (DeviceDimensions.getScreenWidth() - 80)/187; 
     // indicate aspect of desired crop 
     cropIntent.putExtra("aspectX", asp); 
     cropIntent.putExtra("aspectY", 3); 
     // indicate output X and Y 
     cropIntent.putExtra("outputX", DeviceDimensions.getScreenWidth() - 80); 
     cropIntent.putExtra("outputY", 187*3); 
     // retrieve data on return 
     cropIntent.putExtra("return-data", true); 
     // start the activity - we handle returning in onActivityResult 
     startActivityForResult(cropIntent, PIC_CROP); 
    } 
    // respond to users whose devices do not support the crop action 
    catch (ActivityNotFoundException anfe) { 
     Toast toast = Toast 
       .makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
} 

而且OnActivity結果進行圖像裁切是:

if (requestCode == PIC_CROP) { 
      // get the returned data 
      Bundle extras = data.getExtras(); 
      // get the cropped bitmap 
      Bitmap thePic = extras.getParcelable("data"); 
      parentimage.setImageBitmap(thePic); 
     } 
+2

請注意,裁剪功能對於Android不是強制性的,有些設備可能沒有'com.android.camera.action.CROP'。所以使用外部裁剪功能是個不錯的主意。我最好找一個作物的圖書館,並使用它。 –

+0

好的。 Thnx但爲什麼會發生這種情況,在相機的情況下? – Vinay

+0

[未發現可處理Intent com.android.camera.action.CROP的活動]的可能重複(https://stackoverflow.com/questions/41890891/no-activity-found-to-handle-intent-com-android- camera-action-crop) – W4R10CK

回答

0

這可能是命令後回來以不同的格式。 intents中的文件可以隱藏在幾個不同的地方,可以通過幾種不同的方式訪問這些文件,從而使流引導。還有一些有權限的項目。相機意圖可能會以一種方式爲您提供數據,作物意圖可能會以不同的方式返回裁剪後的數據。所以你不能指望兩者是相同的。你必須覆蓋所有的基礎。

請記住,這只是您設備的CROP功能。其他設備沒有裁剪功能,它們也可能工作不同。我不相信他們中的很多人。它實際上是繪製盒子並在另一個位圖上繪製位圖。我使用第三方庫並將其包含在您的應用中。然後你可以確定它的工作。

雖然聽起來不錯。您無法確定該功能是否存在。更少的是它將以一致的方式工作。

如果我記得文件可以在文件流中的額外內容。它可以是content://或file://對象。而權限可能會變得三元。畫廊傾向於將它們作爲content://文件返回,而不命名後綴,而相機可能會給出可以讀取的文件後綴。

我已經看過這個東西幾次,你需要查找從圖庫返回的東西的名稱知道文件類型,而其他時候,它包含的URI正確給你正確的後綴。

這些和其他原因基本上使作物意圖功能沒有價值。如果我不得不猜測文件存儲方式與您從裁剪返回時所期望的方式不同。我使用類似:

public Uri getUriFromIntent(Intent intent) { 
    Uri uri = intent.getData(); 
    if (uri != null) return uri; 

    Bundle bundle = intent.getExtras(); 
    if (bundle == null) return null; 

    Object object = bundle.get(Intent.EXTRA_STREAM); 
    if (object instanceof Uri) { 
     return (Uri) object; 
    } 
    return null; 
} 

在尋找的希望在那裏它可能是,因爲不同的事情將文件放在一堆不同的地方,它簡直瘋了試圖找到他們,如果你不知道確切的服務這給了你的URI。