Android 将google drive pdf上载到服务器
我想将pdf上传到我的服务器。为此,我想允许用户从本地设备以及google drive中选择文件 若用户从他们的设备中选择文件,那个么我会得到类似的uri content://com.android.providers.downloads.documents/document/5447 但当用户从他们的谷歌硬盘中选择文件时,我得到的uri就像 content://com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3D10324 我无法将此uri转换为文件 选择文件的代码Android 将google drive pdf上载到服务器,android,file,pdf,retrofit2,Android,File,Pdf,Retrofit2,我想将pdf上传到我的服务器。为此,我想允许用户从本地设备以及google drive中选择文件 若用户从他们的设备中选择文件,那个么我会得到类似的uri content://com.android.providers.downloads.documents/document/5447 但当用户从他们的谷歌硬盘中选择文件时,我得到的uri就像 content://com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3D10324
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(Intent.createChooser(intent,"Choose File to Upload.."),PICK_FILE_REQUEST);
}
创建多部分的代码
公共MultipartBody.Part GetMultiPartCV(Uri){
当我从下载的文件夹上传时,它工作得很好,因为我可以将uri转换为文件,但当我从google drive选择filr时,我无法将uri转换为文件,因此它会给我提供空指针异常。如果你找到了这个问题的答案,请标记我。我面临着同样的问题,我已经下载了文件,然后上传到服务器。你能显示吗请给我一些链接。@Rajat你能在这里添加你的答案吗?我的朋友,我现在没有代码。
if (uri!= null) {
File file = FileUtils.getFile(this, uri);
RequestBody requestFile = RequestBody.create(MediaType.parse(getContentResolver().getType(uri)), file);
return MultipartBody.Part.createFormData("file", file.getPath(), requestFile);
} else {
return null;
}
}