简体   繁体   中英

Unable to upload files (using ALAsset libraries) to dropbox using the dropbox API

In my iOS App, I am trying to use the "uploadFile" call provided by Dropbox. I am using ALAssetsLibrary to choose a particular Photo/Video obtained programatically as an Asset. I need to upload that particular asset to dropbox.

//ALAsset *asset is the asset i intend to upload
[self.restClient uploadFile:[asset.defaultRepresentation filename] toPath:@"/" withParentRev:nil fromPath:[asset.defaultRepresentation.url absoluteString]];

Dropbox doesn't seem to like the path that the asset carries, which looks something like this : "assets-library://asset/asset.PNG?..."

I have enabled location services for the app and i am able to list all the assets as well.

The upload call throws the error, "[WARNING] DropboxSDK: File does not exist (assets-library://asset/asset.PNG?id=5DC234C1-B27E-45E2-BE61-46E9A266C818&ext=PNG)", in the logs.

The uploadFileFailedWithError function gets called with a error - ("Error Domain=dropbox.com Code=1001 "The operation couldn't be completed. (dropbox.com error 1001.")

Is there anything wrong about the way i am sending the uploadFile call, especially the "from" path?? Any help will be greatly appreciated.

You only get a reference, and not a valid file url, which is useless to classes outside of AssetsLibrary . However, you can quite easily get the asset data and persist it to disk yourself:

- (void)writeAsset:(ALAsset *)asset toPath:(NSString *)path
{
    ALAssetRepresentation *representation = asset.defaultRepresentation;
    long long size = representation.size;
    NSMutableData *rawData = [[NSMutableData alloc] initWithCapacity:size];
    void *buffer = [rawData mutableBytes];
    [representation getBytes:buffer fromOffset:0 length:size error:nil];
    NSData *assetData = [[NSData alloc] initWithBytes:buffer length:size];
    [assetData writeToFile:path atomically:YES];
}

@hwaxxer's solution works perfectly for small assets. If you want to store asset of any size and avoid memory consumption problems have a look at this answer:

SO: Get video NSData from ALAsset url iOS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM