null呼叫ACTION_PICK

时间:2017-04-04 17:58:15

标签: android android-intent android-gallery

我以这样的意图打电话给画廊:

        Intent picturePickerIntent = new Intent(Intent.ACTION_PICK,
            MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    picturePickerIntent.setType("image/*");
    picturePickerIntent.putExtra("crop", "true");
    picturePickerIntent.putExtra("aspectX", 1);
    picturePickerIntent.putExtra("aspectY", 1);
    picturePickerIntent.putExtra("outputX", PHOTO_SIZE);
    picturePickerIntent.putExtra("outputY", PHOTO_SIZE);
    picturePickerIntent.putExtra("return-data", true);

    startActivityForResult(picturePickerIntent, IMAGE_GALERY_REQUEST);

然后我得到了结果:

if (resultCode == RESULT_OK) {
        if (requestCode == IMAGE_GALERY_REQUEST) {
            Bundle extras = data.getExtras();
            if (extras != null) {
                bm = extras.getParcelable("data");
                photoView.setImageBitmap(bm);
                if (avatarSelected != null) {
                    avatarSelected.setBackgroundColor(Color.TRANSPARENT);
                }
                this.avatarSelected = null;
            }else{
                // Sometimes, the photo isn't loaded
                Toast.makeText(this, "The photo has not been loaded", Toast.LENGTH_SHORT).show();
            }

在某些设备上,此代码的工作方式类似于魅力,而在其他设备上,它每次都会显示吐司,而在6.0仿真器上,它只能在两次显示中使用。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

代码似乎没有问题,但你忘记了动态权限,它总是返回null。这个here

if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {


    } else {

        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

    }
}

基本上你需要为android 6和plus动态请求权限。