0
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_WRITE_EXTERNAL && resultCode == RESULT_OK) {
        if (data.getClipData() != null) {
            int count = data.getClipData().getItemCount();
            for (int i = 0; i < count; i++) {
                Uri uri = data.getClipData().getItemAt(i).getUri();
                fileArrayList.add(uri);
            }
            //replaceFragment(fileArrayList);
        } else {
            if (data.getData()!=null){
                Uri uri = data.getData();
                fileArrayList.add(uri);
                Toast.makeText(getContext(),""+fileArrayList.size(),Toast.LENGTH_SHORT).show();
                //replaceFragment(fileArrayList);
            }
        }
    }
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_WRITE_EXTERNAL) {
        if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //Toast.makeText(this, "Read Contacts permission granted", Toast.LENGTH_SHORT).show();
            SelectImage();
        } else {
            Toast.makeText(getContext(),"Error!",Toast.LENGTH_SHORT).show();
        }
    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
//Select Image Method
public void SelectImage() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Image(s)"), REQUEST_WRITE_EXTERNAL);
}

//Storage Permission Method
public void getStoragePermission() {
    if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        }
        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_EXTERNAL);
    }
}

我已经创建了底部工作表对话框片段,当我单击该按钮时,它有一个按钮权限弹出窗口打开,并且在允许存储权限后应该打开画廊,但它不会。当我再次单击按钮时,画廊就会打开。上面提到的方法都不起作用。

4

0 回答 0