从图库返回后,未在Fragment中调用onActivityResult

时间:2019-07-10 10:49:14

标签: android android-permissions

在片段中,我从画廊中选择照片

在Manifest.xml中

   <uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_GPS"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.GET_TASKS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

因为这是我的应用显示权限对话框。我按允许

enter image description here

public class EditProfileFragment extends androidx.fragment.app.Fragment {
 private void choosePhotoFromGallery() {
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}

但是方法

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (BuildConfig.DEBUG)
            Log.d(TAG, "onActivityResult_requestCode = " + requestCode + ", resultCode = " + resultCode + ", data = "
                    + AndroidUtil.intent2String(data));
返回我的应用后,在片段

不致电

不打印文字:onActivityResult_requestCode

但是在 Activity 中调用onActivityResult(int requestCode,int resultCode,Intent data)方法。 但是我需要分片调用此方法。

此处请求权限对话框:

  private boolean checkAndRequestPermissions() {
        int writeExternalStorage = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
        int readExternalStorage = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
        int camera = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA);
        List<String> listPermissionsNeeded = new ArrayList<>();
        if (writeExternalStorage != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
        if (readExternalStorage != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
        }
        if (camera != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.CAMERA);
        }
        if (BuildConfig.DEBUG)
            Log.d(TAG, "checkAndRequestPermissions_listPermissionsNeeded(" + listPermissionsNeeded.size() + ")");
        if (!listPermissionsNeeded.isEmpty()) {
            if (BuildConfig.DEBUG)
                Log.d(TAG, "checkAndRequestPermissions_REQUEST_WRITE_EXTERNAL_STORAGE_REQ_CODE");
            ActivityCompat.requestPermissions(getActivity(), listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()])
                    , WRITE_EXTERNAL_STORAGE_REQ_CODE);
            return false;
        }
        return true;
    }

2 个答案:

答案 0 :(得分:3)

将其放入“活动结果”中的活动

   Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frameLayout);
        fragment.onActivityResult(requestCode, resultCode, data);

它将要做的是调用片段的片段onActivityResult。这样您的片段中就会有选择数据

结果的内部片段开始活动

getActivity().startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);


现在处于活动状态

   @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frameLayout);
        fragment.onActivityResult(requestCode, resultCode, data);
    }

内部片段

 @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        //  super.onActivityResult(requestCode, resultCode, data);
        Log.d(TAG, "Activit Result");
        if (resultCode == -1) {
        }
}

希望有帮助

答案 1 :(得分:1)

由于您是从片段中拨打电话,因此请尝试

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.your_tag);
    fragment.onActivityResult(requestCode, resultCode, data);
}