2016-02-05 71 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

相关问题