2014-02-28 64 views
1

我試過打開郵件附件。接收URL在下面的方法使用Apportable無法在Android上打開郵件附件

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 

,但不能從這個網址

NSData *data = [NSData dataWithContentsOfFile:[url path]] 
// data is nil 

接收到的URL

content://gmail-ls/[email protected]/messages/1/attachments/0.0/BEST/false 

回答

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]]; 
} 
相關問題