Android文件选择器未给出正确的URI
创始人
2024-10-12 06:01:12
0

在 onActivityResult() 方法中使用 Intent.getData() 检查返回的数据。如果 Intent 包含 EXTRA_STREAM,可以使用 getContentResolver().openInputStream() 方法打开文件并将其复制到应用程序的私有存储区域。

示例代码如下:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_OK) {
        return;
    }

    Uri uri = data.getData();

    if (uri != null) {
        // Handle the file selection
    } else if (data.getExtras() != null && data.getExtras().containsKey(Intent.EXTRA_STREAM)) {
        try {
            InputStream stream = getContentResolver().openInputStream((Uri) data.getExtras().get(Intent.EXTRA_STREAM));
            File file = new File(getFilesDir(), "selected_file");
            OutputStream output = new FileOutputStream(file);

            byte[] buffer = new byte[4 * 1024]; // or other buffer size
            int read;

            while ((read = stream.read(buffer)) != -1) {
                output.write(buffer, 0, read);
            }

            output.flush();
            output.close();
            stream.close();

            // Handle the copied file
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

相关内容

热门资讯

透视游戏"wepok... 透视游戏"wepoker免费脚本咨询"详细辅助教你教程(辅助分析器技巧);亲真的是有正版授权,小编(...
透视辅助"wepok... 透视辅助"wepoker有没有挂"详细辅助玩家教程(私人局有透视神器)关于wepoker有没有挂机制...
透视好友房"wepo... 透视好友房"wepoker辅助软件视频"详细辅助微扑克教程(软件透视);玩家必备必赢加哟《13670...
透视了解"hhpok... 透视了解"hhpoker免费辅助器"详细辅助技巧教程(作弊辅助方法);科技安装教程;13670430...
透视攻略"wepok... 此外,数据分析德州(wepoker免费脚本咨询)辅助神器app还具备辅助透视行为开挂功能,通过对客户...
透视辅助"wpk作弊... 透视辅助"wpk作弊"详细辅助新2025版(黑侠辅助器正版下载神器)是一款可以让一直输的玩家,快速成...
透视规律"wepok... 透视规律"wepoker买钻石有用吗"详细辅助德州论坛(免费透视脚本攻略);1分钟了解详细教程(微信...
透视脚本"wepok... 透视脚本"wepoker透视破解版"详细辅助玩家教你(辅助软件视频神器);实战中需综合运用上述技巧,...
透视真的"wepok... 透视真的"wepoker轻量版有透视吗"详细辅助AI教程(插件攻略)1、不需要AI权限,帮助你快速的...
透视攻略"wepok... 透视攻略"wepoker有脚本吗"详细辅助扑克教程(透视器免费技巧);玩家必备必赢加哟《136704...