AndroidActivityResultContracts.CreateDocumentonResultisnevercalled
创始人
2024-10-06 05:32:01
0

在使用ActivityResultContracts.CreateDocument启动系统文件选择器时,onActivityResult的回调函数中没有调用onResult,因此无法获得所选文件的Uri。解决方法是在启动ActivityResultContracts.CreateDocument时,将返回的Uri保存在一个变量中,并在onActivityResult回调函数中手动调用onResult方法。

以下是示例代码:

private ActivityResultLauncher mDocumentLauncher;

private Uri mSelectedDocumentUri;

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

mDocumentLauncher = registerForActivityResult(new ActivityResultContracts.CreateDocument(), uri -> {
    mSelectedDocumentUri = uri;
    // manually call onResult
    onDocumentSelected(uri);
});

// start file picker
mDocumentLauncher.launch("my_document.txt");

}

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);

if (requestCode == MY_DOCUMENT_REQUEST_CODE) {
    if (resultCode == RESULT_OK) {
        // do nothing, onResult is already called manually
    } else {
        // handle cancel
    }
}

}

private void onDocumentSelected(Uri uri) { // handle selected uri here Log.d(TAG, "Selected document URI: " + uri); }

需要注意的是,在手动调用onResult时需要保证在主线程中执行。

相关内容

热门资讯

玩家必看教程!wepoker透... 玩家必看教程!wepoker透视脚本免费下载pc,拱趴大菠萝机器人,AI教程(有挂软件);建议优先通...
来一盘!we poker辅助器... 来一盘!we poker辅助器v3.3,aapoker透视脚本入口,力荐教程(有挂透明);小薇(透视...
推荐十款!hhpoker免费透... 推荐十款!hhpoker免费透视脚本,wepoker黑侠辅助器,教你教程(有挂透明)科技教程也叫必备...
玩家交流!aapoker怎么拿... 玩家交流!aapoker怎么拿好牌,hh poker软件,扑克教程(有挂神器);原来确实真的有挂(需...
玩家必备科普!wepoker安... 玩家必备科普!wepoker安装教程,wepoker轻量版透视,攻略方法(有挂软件);实战中需综合运...
分享认知!hhpoker透视脚... 分享认知!hhpoker透视脚本,德州透视脚本,攻略方法(有挂攻略);玩家必备必赢加哟《136704...
总算清楚!德普之星怎么开辅助,... 总算清楚!德普之星怎么开辅助,wpk刷入池率脚本,2025教程(有挂技巧)科技教程也叫必备教程,这是...
科技通报!pokemmo手机脚... 科技通报!pokemmo手机脚本,newpoker怎么安装脚本,2025新版教程(有挂技巧)这是由厦...
三分钟了解!红龙poker辅助... 三分钟了解!红龙poker辅助器免费观看,wepokerplus脚本,技巧教程(有挂教程);亲真的是...
科技通报!hhpoker德州透... 科技通报!hhpoker德州透视挂,wejoker内置辅助,科技教程(有挂方法);小薇(透视辅助)致...