2013-07-29 67 views
3

我的Galaxy Nexus現在在Android 4.3上運行,允許我使用此新版本測試我的應用程序。一切似乎都很好,除了裁剪。Android 4.3 crop gallery resultCode取消

我有一個應用程序,使用相機拍照,然後通過畫廊應用程序裁剪圖像。

我也可以從畫廊中選擇一張圖片並剪下。 自Android 4.3以來,圖庫應用程序發生了變化。

如果我採取與照相機API圖片,然後問畫廊裁剪它在我onActivityResult方法發送resultCode設置爲0(意思是取消),而我從作物意見「保存」點擊。

但是,如果我從圖庫中選擇一張圖片並裁切一切正常,則resultCode參數設置爲-1。 我在這兩種情況下調用相同的方法來裁剪圖片。

我在手機上有quickpic(畫廊應用程序的替代品),它具有一切功能!

private void performCrop(Uri picUri) { 
    try { 
     int aspectX = 750; 
     int aspectY = 1011; 

     Intent intent = new Intent("com.android.camera.action.CROP"); 
     intent.setDataAndType(picUri, "image/*"); 
     intent.putExtra("crop", "true"); 
     intent.putExtra("scale", "true"); 
     intent.putExtra("aspectX", aspectX); 
     intent.putExtra("aspectY", aspectY); 
     intent.putExtra("scaleUpIfNeeded", true); 

     intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCurrentPhotoPath))); 

     startActivityForResult(intent, CROP); 
    } 
    catch (ActivityNotFoundException anfe) { 
     String errorMessage = "Your device doesn't support the crop action!"; 
     Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
} 

一切工作正常在Android 4.2.2。 謝謝你的幫助!

回答

3

你有沒有考慮只使用一個圖書館像這樣的:

https://github.com/biokys/cropimage

我找到com.android.camera.action.CROP有時從電話的行爲不同,以電話並不總是可用的,所以如果你想發佈它,它可能會給你帶來一些問題。

UPDATE:

我已經測試了上述庫與Android 4.3,它沒有問題的作品。您只需將庫添加到您的項目。

然後你可以寫你的方法非常相似的方式:

private void performCrop(Uri picUri) { 
//you have to convert picUri to string and remove the "file://" to work as a path for this library 
String path = picUri.toString().replaceAll("file://", ""); 

try { 
    int aspectX = 750; 
    int aspectY = 1011; 

    Intent intent = new Intent(this, CropImage.class); 
    //send the path to CropImage intent to get the photo you have just taken or selected from gallery 
    intent.putExtra(CropImage.IMAGE_PATH, path); 

    intent.putExtra(CropImage.SCALE, true); 

    intent.putExtra("aspectX", aspectX); 
    intent.putExtra("aspectY", aspectY); 

    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCurrentPhotoPath))); 

    startActivityForResult(intent, CROP); 
} 
catch (ActivityNotFoundException anfe) { 
    String errorMessage = "Your device doesn't support the crop action!"; 
    Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); 
    toast.show(); 
} 

}

+0

謝謝你的作品太棒了! :D – Kerwan

+0

沒問題,很高興爲你工作:) – Daveloper87

+0

Thx很棒。但是你能解釋一下Android 4.3上的這種行爲嗎?我在Samsung s3設備的Android 4.1上也看到了這種奇怪的行爲。 – noxius

3

以上圖書館是唯一有用的,如果你的裁剪成很多小的圖像。如果你想裁剪更好的分辨率圖像,最好使用Android Crop Intent。

picUri必須是指向圖像的有效URI,並且outputUri應該是您爲寫入裁剪圖像而創建的新文件。它適用於所有設備,並且4.3源代碼的確可以使用com.android.camera.action.CROP intent。我已經在很多設備上測試過它,它運行良好。

private void performCrop(Uri picUri, Uri outputUri) { 
    try { 
     int aspectX = 2000; 
    int aspectY = 1200; 

     Intent intent = new Intent("com.android.camera.action.CROP"); 
     intent.setDataAndType(picUri, "image/*"); 
     intent.putExtra("scale", "true"); 
     intent.putExtra("aspectX", aspectX); 
     intent.putExtra("aspectY", aspectY); 
     intent.putExtra("scaleUpIfNeeded", true); 
     intent.putExtra("return-data", false); 

     intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri); 

     startActivityForResult(intent, CROP); 
    } 
    catch (ActivityNotFoundException anfe) { 
     String errorMessage = "Your device doesn't support the crop action!"; 
     Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
} 
0

我也在Nexus 10上遇到過這個問題。作物意圖返回一個取消的代碼。經過一些調整,我找到了一個解決方案:

在我的情況下,在setDataAndType()中設置的輸入文件與使用MediaStore.EXTRA_OUTPUT多餘的輸出文件設置的文件相同。在大多數設備上使用相同的文件進行輸入和輸出,特別是在4.3以下的設備上。然而,4.3會導致作物被取消。簡單地使用不同的文件輸入和輸出解決了這個問題。

因此,您需要確保的是您的picUri參數指向的文件與您的mCurrentPhotoPath不相同。我不確定從4.2到4.3有什麼變化導致這個問題。但使用不同的文件似乎很容易解決它。