This documentation is archived and is not being maintained.

Move, copy, create, or delete a file or folder (Android)

You can move, copy, create, or delete a user's files or folders on Microsoft OneDrive, from your Android app.

We assume that the user has already signed in and consented to the wl.skydrive scope for reading file or folder properties. If you have not added user sign-in to your Android apps, see Signing users in (Android).

We assume that you are familiar with Java, and can write Android apps.

Note Note

You can't create files directly on OneDrive. Instead, you upload files to OneDrive. To upload files, see Downloading and uploading files (Android).

To move or copy a file or folder, use code like this. The wl.skydrive_update scope is required. Change the file ID to the file ID or folder ID of the file or folder that you want to move or copy. Change the folder ID to the folder ID of the target folder to which you want to move or copy the source file or folder.

Note Note

For move operations, the source can be anything except the root, and the destination must be the folder to which the item will be moved.

The destination of a move or copy operation must be a folder. Folders themselves cannot be copied. Also, OneDrive storage is structured so that move and copy operations cannot occur between the OneDrive folder hierarchies of different users. For example, even if user A can read user B's files, user A cannot copy or move them to his or her own OneDrive folders.

In this example, the moveAsync method is used to move the file. The onComplete and onError methods are used to determine whether the call was successful, and if so, display a confirmation message.

To copy a file, replace moveAsync with copyAsync in the example.

public void moveFile() {
    client.moveAsync("file.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!120", 
            "folder.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!145", new LiveOperationListener() {
        public void onComplete(LiveOperation operation) {
            resultTextView.setText("File moved.");
        }
        public void onError(LiveOperationException exception, LiveOperation operation) {
            resultTextView.setText("Error moving file: " + exception.getMessage());
        }
    });
}

To remove a file, photo, video, or audio, use code like this. The wl.skydrive_update scope is required. Change the file ID to a different file ID, photo ID, video ID, or audio ID to remove a different file, photo, video, or audio.

public void deleteFile() {
    final String fileId = "file.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!125";
    client.deleteAsync(fileId, new LiveOperationListener() {
        public void onError(LiveOperationException exception, LiveOperation operation) {
               resultTextView.setText("Error deleting file: " + exception.getMessage());
           }
           public void onComplete(LiveOperation operation) {
            resultTextView.setText("File deleted.");
           }
       });
    
}

To add a new folder, use code like this. The wl.skydrive_update scope is required. Change me/skydrive to the folder ID (or, in certain cases, the friendly name like "My Pictures") of the folder in which the new folder will be created.

public void createFolder() {
    final LiveOperationListener opListener = new LiveOperationListener() {
        public void onError(LiveOperationException exception, LiveOperation operation) {
               resultTextView.setText("Error creating folder: " + exception.getMessage());
           }
        public void onComplete(LiveOperation operation) {
            JSONObject result = operation.getResult();
            String text = "Folder created:\n" +
                "\nID = " + result.optString("id") +
                "\nName = " + result.optString("name");
               resultTextView.setText(text);
           }
       };
    auth.login(this, Arrays.asList(new String[] { "wl.skydrive_update" }), 
        new LiveAuthListener() {
               public void onAuthError(LiveAuthException exception, Object userState) {
                   resultTextView.setText("Error signing in: " + exception.getMessage());
               }
            public void onAuthComplete(LiveStatus status, LiveConnectSession session, Object userState) {
                try {
                    JSONObject body = new JSONObject();
                    body.put("name", "MyBrandNewFolder");
                    body.put("description", "My brand new folder");
                       client.postAsync("me/skydrive", body, opListener);    
                }
                catch(JSONException ex) {
                    resultTextView.setText("Error building folder: " + ex.getMessage());
                }
               }
        }
    ); 
}

To remove a folder, use code like this. The wl.skydrive_update scope is required.

public void deleteFile() {
    final String fileId = "file.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!125";
    client.deleteAsync(fileId, new LiveOperationListener() {
        public void onError(LiveOperationException exception, LiveOperation operation) {
               resultTextView.setText("Error deleting file: " + exception.getMessage());
           }
           public void onComplete(LiveOperation operation) {
            resultTextView.setText("File deleted.");
           }
       });
    
}

For details about the minimum required and optional structures that your app must provide to use POST, see the Folder object.

Show: