我用的Android SDK來解決這一問題得到數據。你可以看看如何使用java與apportable here。
這是我的Java代碼
static private byte[] readBytes(InputStream inputStream) throws IOException {
// this dynamically extends to take the bytes you read
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
// this is storage overwritten on each iteration with bytes
int bufferSize = 1048576;
byte[] buffer = new byte[bufferSize];
// we need to know how may bytes were read to write them to the byteBuffer
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
// and then we can return your byte array.
return byteBuffer.toByteArray();
}
public byte[] dataFromUrl(String path, Activity activity) {
Uri uri = Uri.parse(path);
InputStream is = null;
byte[] data = null;
try {
is = activity.getContentResolver().openInputStream(uri);
data = readBytes(is);
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
和Objective-C部分
+ (void)initializeJava
{
[super initializeJava];
// here your initialize code
...
[KeyboardBridge registerInstanceMethod:@"dataFromUrl"
selector:@selector(dataFromUrl:forActivity:)
returnValue:[NSData className]
arguments:[NSString className], [AndroidActivity className], nil];
}
- (NSData *)dataFromUrl:(NSString *)path {
return [self dataFromUrl:path forActivity:[AndroidActivity currentActivity]];
}