2016-02-05 96 views
-1

這是我從圖庫中選擇圖片和裁剪的意圖代碼。裁剪意圖在Android中無法正常工作

int PICK_IMAGE_REQUEST = 100; 
        Intent intent = new Intent(); 
        intent.setType("image/*"); 
        intent.setAction(Intent.ACTION_PICK); 
        intent.putExtra(MediaStore.EXTRA_OUTPUT, 
          MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString()); 
        intent.putExtra("crop", "true"); 
        intent.putExtra("aspectX", 150); 
        intent.putExtra("aspectY", 150); 
        intent.putExtra("outputX", 150); 
        intent.putExtra("outputY", 150); 
        intent.putExtra("return-data", true); 
        getActivity().startActivityForResult(Intent.createChooser(intent, 
          "Complete using with."), PICK_IMAGE_REQUEST); 

這裏是我的onActivityResult

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    int PICK_IMAGE_REQUEST = 100; 
    Bundle extras = null; 
    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK) { 
     extras = data.getExtras(); 
    } 
    if (extras != null) { 
     Bitmap photo = extras.getParcelable("data"); 
     ImageView profilePhoto = (ImageView) findViewById(R.id.profileImageView); 
     profilePhoto.setImageBitmap(photo); 
    } 

這些碼以上的作物&集圖像成功。但是,有時它不能正常工作。我的意思是當我使用第三方圖庫應用程序而不是使用設備的默認圖庫應用程序。它不設置圖像。當使用其他圖庫應用程序時,這可能無法正確獲取文件路徑。那麼,如何實現選擇&裁剪並將圖像設置爲imageview?我研究了互聯網,但迄今爲止沒有解決這個問題。

回答

1

這是我選擇圖片和從圖庫中裁剪的意圖代碼。

不,這是選擇圖片的代碼。您在那裏的各種附加內容不屬於the ACTION_PICK documentation或其他官方文檔。

這些代碼以上的作物&集成功圖像

一般不。

但是,有時它不能正常工作。我的意思是當我使用第三方圖庫應用程序而不是使用設備的默認圖庫應用程序。

有成千上萬的Android設備型號。沒有一個單一的「默認畫廊應用程序」爲他們所有;將有幾十個,即使不是數百個「默認圖庫應用程序」實現。沒有人必須支持你正在嘗試的隨機附加功能。另外,沒有必要返回data額外的東西,因爲ACTION_PICK在結果Intent中返回Uri,如在the documentation for ACTION_PICK中所述。

那麼,我該如何實現選擇&裁剪和設置圖像到imageview?

擺脫臨時演員。擺脫extras.getParcelable("data")位。獲取所選圖像的Uridata.getData())。將其與可用於Android的各種image cropping libraries之一結合使用。

+0

我也使用了ACTION_GET_CONTENT,但結果是一樣的。除了你能給我代碼的例子嗎?感謝您的回答 – Musti

+0

@Musti:「我也使用了ACTION_GET_CONTENT,但結果與我寫的一樣」 - 您在那裏的各種附加內容不是ACTION_PICK文檔的一部分**或任何其他官方文檔* *「(強調增加)。對於任何操作字符串,這些附加內容都沒有記錄。 「除了你能給我代碼的例子嗎?」 - 選擇一個具有代碼示例的庫。 – CommonsWare

相關問題