无法使用Apportable在Android上打开邮件附件

时间:2014-02-28 11:56:35

标签: android apportable

我尝试过打开邮件附件。通过以下方法接收网址

- (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

1 个答案:

答案 0 :(得分:3)

我使用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]];
}