Android™ Dropbox: Dropbox Copy a File or Folder

Back to Index

Copy a file or folder to a different location in the user's Dropbox. If the source path is a folder all its contents will be copied.

Documentation: https://www.dropbox.com/developers/documentation/http/documentation#files-copy

CURL Command

curl -X POST https://api.dropboxapi.com/2/files/copy_v2 \
    --header "Authorization: Bearer DROPBOX-ACCESS-TOKEN" \
    --header "Content-Type: application/json" \
    --data "{\"from_path\": \"/ocean/starfish.jpg\",\"to_path\": \"/ocean/starfish2.jpg\",\"allow_shared_folder\": false,\"autorename\": false,\"allow_ownership_transfer\": false}"

Android™ Example

// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    CkRest rest = new CkRest();
    boolean success;

    //  URL: https://api.dropboxapi.com/2/files/copy_v2
    boolean bTls = true;
    int port = 443;
    boolean bAutoReconnect = true;
    success = rest.Connect("api.dropboxapi.com",port,bTls,bAutoReconnect);
    if (success != true) {
        Log.i(TAG, "ConnectFailReason: " + String.valueOf(rest.get_ConnectFailReason()));
        Log.i(TAG, rest.lastErrorText());
        return;
        }

    //  See the Online Tool for Generating JSON Creation Code
    CkJsonObject json = new CkJsonObject();
    json.UpdateString("from_path","/ocean/starfish.jpg");
    json.UpdateString("to_path","/ocean/starfish2.jpg");
    json.UpdateBool("allow_shared_folder",false);
    json.UpdateBool("autorename",false);
    json.UpdateBool("allow_ownership_transfer",false);

    rest.AddHeader("Authorization","Bearer DROPBOX-ACCESS-TOKEN");
    rest.AddHeader("Content-Type","application/json");

    CkStringBuilder sbRequestBody = new CkStringBuilder();
    json.EmitSb(sbRequestBody);
    CkStringBuilder sbResponseBody = new CkStringBuilder();
    success = rest.FullRequestSb("POST","/2/files/copy_v2",sbRequestBody,sbResponseBody);
    if (success != true) {
        Log.i(TAG, rest.lastErrorText());
        return;
        }

    int respStatusCode = rest.get_ResponseStatusCode();
    if (respStatusCode >= 400) {
        Log.i(TAG, "Response Status Code = " + String.valueOf(respStatusCode));
        Log.i(TAG, "Response Header:");
        Log.i(TAG, rest.responseHeader());
        Log.i(TAG, "Response Body:");
        Log.i(TAG, sbResponseBody.getAsString());
        return;
        }

    CkJsonObject jsonResponse = new CkJsonObject();
    jsonResponse.LoadSb(sbResponseBody);

    //  See the Online Tool for Generating JSON Parse Code

    String metadataTag = jsonResponse.stringOf("metadata.\".tag\"");
    String metadataName = jsonResponse.stringOf("metadata.name");
    String metadataPath_lower = jsonResponse.stringOf("metadata.path_lower");
    String metadataPath_display = jsonResponse.stringOf("metadata.path_display");
    String metadataId = jsonResponse.stringOf("metadata.id");
    String metadataClient_modified = jsonResponse.stringOf("metadata.client_modified");
    String metadataServer_modified = jsonResponse.stringOf("metadata.server_modified");
    String metadataRev = jsonResponse.stringOf("metadata.rev");
    int metadataSize = jsonResponse.IntOf("metadata.size");
    String metadataContent_hash = jsonResponse.stringOf("metadata.content_hash");

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}

Sample JSON Response Body

{
  "metadata": {
    ".tag": "file",
    "name": "starfish2.jpg",
    "path_lower": "/ocean/starfish2.jpg",
    "path_display": "/ocean/starfish2.jpg",
    "id": "id:JSXYsxHo1hAAAAAAAAAADg",
    "client_modified": "2018-10-22T22:38:18Z",
    "server_modified": "2018-10-22T22:40:38Z",
    "rev": "1c482db15f",
    "size": 6229,
    "content_hash": "9fa9d692d0762ee759c22dea993c7de0068f9ba2ca842e761a1636d3b0b3cbba"
  }
}