Hitesh Patel
Hitesh Patel

Reputation: 2878

Download all types of file from Dropbox

I am working on Dropbox. I see the documentation. This is my code to display list:

Entry entryCheck = mApi.metadata("/", 100, null, true, null);
Log.i("Item Name", entryCheck.fileName());
Log.i("Is Folder", String.valueOf(entryCheck.isDir));

I got all list from dropbox but my question is that

  1. Here entryCheck.isDir always give me true value if it is file or directory so how i can know which is file or which one is directory?
  2. How i downloaded that files.

I tried with this but it is not working:

private boolean downloadDropboxFile(String dbPath, File localFile,
        DropboxAPI<?> api) throws IOException {
    BufferedInputStream br = null;
    BufferedOutputStream bw = null;
    try {
        if (!localFile.exists()) {
            localFile.createNewFile(); // otherwise dropbox client will fail
            // silently
        }                   

        DropboxInputStream fin = mApi.getFileStream("dropbox", dbPath);
        br = new BufferedInputStream(fin);
        bw = new BufferedOutputStream(new FileOutputStream(localFile));

        byte[] buffer = new byte[4096];
        int read;
        while (true) {
            read = br.read(buffer);
            if (read <= 0) {
                break;
            }
            bw.write(buffer, 0, read);
        }
    } catch (DropboxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        // in finally block:
        if (bw != null) {
            bw.close();
        }
        if (br != null) {
            br.close();
        }
    }

    return true;
}

Upvotes: 2

Views: 2356

Answers (1)

Amel Jose
Amel Jose

Reputation: 883

This will work

String inPath ="mnt/sdcard/"+filename;
File file=new File(inPath);           
try {

    mFos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
    mErrorMsg = "Couldn't create a local file to store the image";
    return false;
}

mApi.getFile("/"+filename, null, mFos, null);

This downloads the file and store it in the sdcard location inPath. You have to do it in a new thread,not in main thread.Use AsyncTask. http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html this link explains why..

Upvotes: 1

Related Questions