可以使用 Android Storage Access Framework 在 Google Drive 上创建文件吗?

问题描述

我知道 a similar question 作者使用 Xamarin。

应用程序使用 SAF 通过启动系统文件选择器在本地存储上创建文本文件,没有任何问题。用户可以使用文件选择器来选择本地文件夹或 Google Drive 文件夹。同样,如果用户选择本地文件夹,它也能正常工作。如果用户选择 Google Drive 文件夹,新文件的创建总是没有任何问题,但它的字节数为零。我已经共享并下载了文件以确认它是空的。

以下是相关代码

launcherExportFileSelection = requireActivity().registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),result -> {
            try {
                if (result.getResultCode() == Activity.RESULT_OK) {
                    Uri uri = result.getData().getData();
                    ParcelFileDescriptor pfd = requireActivity().getContentResolver().openFileDescriptor(uri,"w");
                    if (pfd == null) {
                        
                    } else {
                        FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
                        fileOutputStream.write(stringText.getBytes());
                        fileOutputStream.flush();
                        fileOutputStream.close();
                    }
                } else {
                    
                }
            } catch (Exception ex) {
                
            }
        }); 

我也尝试过如下的 bufferedoutputstream 无济于事:

launcherExportFileSelection = requireActivity().registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),"w");
                    if (pfd == null) {
                        
                    } else {
                        FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
                        bufferedoutputstream bos = new bufferedoutputstream(fileOutputStream);
                        bos.write(stringText.getBytes());
                        bos.flush();
                        bos.close();
                        fileOutputStream.flush();
                        fileOutputStream.close();
                    }
                } else {
                    
                }
            } catch (Exception ex) {
                
            }
        });

不会抛出异常。有人可以对此有所了解吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)