如何知道我的URI是否可以转换为字节数组(android)?

如何知道我的URI是否可以转换为字节数组(android)?,android,http,uri,inputstream,mime,Android,Http,Uri,Inputstream,Mime,好的,我正在制作一个应用程序,允许用户选择一个电子表格上传到服务器。我通过获取URI上传文件,将其转换为inputstream,然后转换为字节数组,并将其作为内容体发送。我使用Android意图获取URI Intent i=新的意图(Intent.ACTION\u GET\u CONTENT) 这是我用来获取字节数组的代码: is = contentResolver.openInputStream(importedFileUri); String responseBody = null;

好的,我正在制作一个应用程序,允许用户选择一个电子表格上传到服务器。我通过获取URI上传文件,将其转换为inputstream,然后转换为字节数组,并将其作为内容体发送。我使用Android意图获取URI

Intent i=新的意图(Intent.ACTION\u GET\u CONTENT)

这是我用来获取字节数组的代码:

is = contentResolver.openInputStream(importedFileUri);
String responseBody = null;
    int len;
    int buffSize = 1024;
    byte[] buf;
    byte[] byteArray = null;
    try {
        if (is != null) {
            if (is instanceof ByteArrayInputStream) {
                buffSize = is.available();

                buf = new byte[buffSize];
                len = is.read(buf, 0, buffSize);
            } else {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                buf = new byte[buffSize];
                while ((len = is.read(buf, 0, buffSize)) != -1) {
                    bos.write(buf, 0, len);
                }
                byteArray = bos.toByteArray();

            }
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
最后

    ContentBody cbFile = new ByteArrayBody(byteArray, fileName);
    MultipartEntity mpEntity = new MultipartEntity();
    mpEntity.addPart("uploadFile", cbFile);
    httppost.setEntity(mpEntity);
对于驱动器/云文件,字节数组(可能还有输入流本身)为空。如何检测用户是否选择了驱动器文件并下载该文件,以便将其转换为字节数组

我为驱动器文件URI记录了以下数据:

URI路径:/document/acc=1;单据=2/单据/会计科目%3D1%3Bdoc%3D2

URI方案:内容

解析类型(内容):application/vnd.google-apps.spreadsheet

分机:空

对于本地文件:

URI路径:/document/549/document/549

URI方案内容

解析类型(内容):application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

分机:xlsx

我得到的信息是这样的:


编辑:
InputStream
本身为空。那么如何访问驱动器/云文件呢?

好的,我通过使用
getAuthority()
方法获得了一些想法,但是没有一致性。驱动器文件可能有类似于
com.android.apps.drive.something
,但本地文件可能是
com.android.externalstorage.something
com.android.providers.downloads.something
。这清楚吗?这是否会因OEM和文件管理员而异?有人知道什么吗?好的,我通过使用
getAuthority()
方法得到了一些想法,但是没有一致性。驱动器文件可能有类似于
com.android.apps.drive.something
,但本地文件可能是
com.android.externalstorage.something
com.android.providers.downloads.something
。这清楚吗?这是否会因OEM和文件管理员而异?有人知道吗?
if (importedFileUri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
                    final MimeTypeMap mime = MimeTypeMap.getSingleton();
                    ext = mime.getExtensionFromMimeType(mActivity.getContentResolver().getType(importedFileUri));
                    System.out.println("resolved type (content) : "+ mActivity.getContentResolver().getType(importedFileUri));
                } else {
                    ext = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(importedFileUri.getPath())).toString());
                    System.out.println("resolved type (other) : "+ Uri.fromFile(new File(importedFileUri.getPath())).toString());
                }