我试过打开邮件附件。通过以下方法接收网址
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
但无法从此网址获取数据
NSData *data = [NSData dataWithContentsOfFile:[url path]]
// data is nil
收到的网址
content://gmail-ls/mymail@gmail.com/messages/1/attachments/0.0/BEST/false
我已经使用Android SDK来解决这个问题。你可以在这里看看如何使用java和apportable。
这是我的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;
}
和目标-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]];
}