Intent i = new Intent(Intent.ACTION_GET_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; doc = 2 / document / acc%3D1%3Bdoc%3D2
URI Scheme:content
已解决的类型(内容):application / vnd.google-apps.spreadsheet
ext:null
这是本地文件:
URI路径:/ document / 549 / document / 549
URI方案内容
已解决的类型(内容):application / vnd.openxmlformats-officedocument.spreadsheetml.sheet
分机:xlsx
我得到了ext(ension),就像这样:
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());
}
编辑:InputStream
本身为空。那么如何访问驱动器/云文件?