Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

android - Dropbox Sharing file URL

I am developing an application for Android and which uses Dropbox for organizing the files. I am exploring the Dropbox API but its description and help is limited, as there is no documentation for the Dropbox API.

I still would like to manage the files to some functionality, for example placing a file and getting a file from Dropbox. Now the problem is when I put some files in Dropbox public folder and I need a URL to share to my contacts in the application. But in the API I could not find any function that returns the web URL of the file to share (Just like in the Deskotop interface of Dropbox, a user can get a Shared URL to send to friends).

Could someone help me figure out how to share that file with contacts in the Application?

Or any other way to share a file using Dropbox Android API?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

According to changes made on DropBox metioned here: https://www.dropbox.com/help/16/en There would be no more Public folders, instead access to files can be done via Share Link.

If you use Android DropBox Core Api then shared link can be retrieved this way:

// Get the metadata for a directory
Entry dirent = mApi.metadata(mPath, 1000, null, true, null);

for (Entry ent : dirent.contents) {

String shareAddress = null;
if (!ent.isDir) {
    DropboxLink shareLink = mApi.share(ent.path);
    shareAddress = getShareURL(shareLink.url).replaceFirst("https://www", "https://dl");
    Log.d(TAG, "dropbox share link " + shareAddress);
}   
}

UPDATE: 2014/07/20 by Dheeraj Bhaskar Use the following helper function alongwith the above function. Since DropBox started to send shortened links it is little bit more problematic to get proper link. For now, I am using this method :

We simply load the URL, follow the redirects and get the new URL.

    String getShareURL(String strURL) {
    URLConnection conn = null;
    String redirectedUrl = null;
    try {
        URL inputURL = new URL(strURL);
        conn = inputURL.openConnection();
        conn.connect();

        InputStream is = conn.getInputStream();
        System.out.println("Redirected URL: " + conn.getURL());
        redirectedUrl = conn.getURL().toString();
        is.close();

    } catch (MalformedURLException e) {
        Log.d(TAG, "Please input a valid URL");
    } catch (IOException ioe) {
        Log.d(TAG, "Can not connect to the URL");
    }

    return redirectedUrl;
}

Note: All of this should be done of course in AsyncTask or Thread. This will produce proper links ready to download

Update 2014/07/25: Change in dropbox share URLs
A heads-up on the kind of URLs to expect
From the Dropbox team:

We wanted to give you a heads up about an upcoming change to the URL structure of Dropbox shared links. While not part of the API, the change could affect apps that manipulate the URLs returned from the /shares endpoint or the "preview" link type returned by the Chooser Drop-in.

Links returned will now have a ?dl=0 appended to them.

E.g., instead of https://www.dropbox.com/s/99eqbiuiepa8y7n/Fluffbeast.docx, you'll receive URLs like this link https://www.dropbox.com/s/99eqbiuiepa8y7n/Fluffbeast.docx?dl=0.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...