如何从Google Drive Android应用程序的私人文件夹获得对文件的部分写访问权限?

如何从Google Drive Android应用程序的私人文件夹获得对文件的部分写访问权限?,android,google-drive-api,android-contentresolver,google-drive-android-api,parcelfiledescriptor,Android,Google Drive Api,Android Contentresolver,Google Drive Android Api,Parcelfiledescriptor,我的设备上安装了API级别为21的。我想允许我的应用程序从我设备上的Google Drive私人文件夹中读取一些文件,进行一些更改,并让Google Drive Android应用程序对其进行同步。单击某个文件会启动一个文件,并显示在该文件之后。正如所指出的,Google Drive Android应用程序不再以file://...下载到其他应用程序。它发出一个content://...相反。尽管如此,我还是可以 ParcelFileDescriptor pfd = getContentReso

我的设备上安装了API级别为21的。我想允许我的应用程序从我设备上的Google Drive私人文件夹中读取一些文件,进行一些更改,并让Google Drive Android应用程序对其进行同步。单击某个文件会启动一个文件,并显示在该文件之后。正如所指出的,Google Drive Android应用程序不再以file://...下载到其他应用程序。它发出一个content://...相反。尽管如此,我还是可以

ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(dataUri, "r");
拥有pfd我可以通过以下方式读取缓冲区中某个位置的一些字节:

FileInputStream fis = new FileInputStream(pfd.getFileDescriptor());
FileChannel fileChannel = fis.getChannel();
fileChannel.position(position);
int bytesRead = fileChannel.read(buffer);
当我试图将缓冲区中的一些字节写入Google Drive私有文件夹中文件的某个位置时,问题出现了:

ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(dataUri, "w"/*"rw", "rwt"*/); //Exception here for "rw"
FileOutputStream fos = new FileOutputStream(pfd.getFileDescriptor()); 
FileChannel fileChannel = fos.getChannel();
fileChannel.position(position);   //Exception here for "w" and "rwt"
int bytesWrite = fileChannel.write(buffer);
这给了我很多。“rwt”模式也有同样的例外。访问模式“rw”给出了其他例外情况:

java.io.FileNotFoundException: Unsupported mode: rw: 
清单中存在所有必要的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
问题是:为什么谷歌让我们有机会重写私有谷歌硬盘文件夹中的整个文件,而不让我们有机会更改该文件中的几个字节?


另外,为了简洁起见,省略了所有必要的try-catch块

您是否尝试过使用“w”模式调用getContentResolver().openFileDescriptor?是的,我尝试过。我的问题不清楚,我现在已经做了一点修改。我猜Drive不允许您更改内容,因为它需要这些内容(例如,为其他客户服务)@Anatoli:你是Google的吗?我猜Google Drive将整个文件存储在数据库中的一个二进制大对象中,如果多个客户端试图进行更改,在REST API中处理文件的写入位将是一场噩梦。
FileInputStream fis = new FileInputStream("/storage/sdcard0/aaa/www/flower.jpg");
ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(dataUri, "rwt");
FileOutputStream fos = new FileOutputStream(pfd.getFileDescriptor());
new StreamCopyThread(fis, fos).start();