Read, create, or delete tags (Android)

Your Android apps can read, create, and delete tags (a type of comment) that users can add to photos, videos, and audio.

In this article
Prerequisites
Read a tag
Create a tag
Delete a tag
Remarks

Prerequisites

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.

Read a tag

To get info about tags, use code like this. The wl.skydrive scope is required. You can change the photo ID to a different photo ID, video ID, or audio ID, to get info about the tags for another photo, video, or audio. Specify a tag's ID to get info about that individual tag.

You can also:

  • Get a limited number of tags by using the limit parameter in the example code to specify the number of items to get. For example, to get the first two tags, replace file.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!131/tags with PHOTO_ID/tags?limit=2 in the example below.

  • Set the first tag to start getting by using the offset parameter in the preceding code to specify the index of the first item to get. For example, to get two items starting at the third item, use PHOTO_ID/tags?limit=2&offset=3.

    Note

    In the JavaScript Object Notation (JSON)-formatted object that's returned, you can look in the paging object for the previous and next structures to get the offset and limit parameter values of the previous and next entries.

public void readTags() {
	String photoId = "file.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!131/tags";
	   client.getAsync(photoId , new LiveOperationListener() {
		public void onError(LiveOperationException exception, LiveOperation operation) {
			   resultTextView.setText("Error reading tags: " + exception.getMessage());
		   }
		public void onComplete(LiveOperation operation) {
			JSONObject result = operation.getResult();
			String msg = "Tag info:\n";
			JSONArray  items = result.optJSONArray("data");
			for (int i = 0; i < items.length(); i++) {
				JSONObject tag = items.optJSONObject(i);
				JSONObject user = tag.optJSONObject("user");
				msg += user.optString("name") + "\n";
			}
			   resultTextView.setText(msg);
		   }
	   });
}

Create a tag

To create a tag, use code like the following. Change the photo ID to a different photo ID, video ID, or audio ID, to add a tag to another photo or video. The wl.skydrive_update scope is required.You can create tags for items that are owned by, or shared with, the signed-in user.

  • To create a tag for an item that is owned by the signed-in user, replace the photo ID with the item's ID in the code that follows.

  • To create a tag for an item that is shared with the signed-in user, first use the wl.contacts_skydrive scope to make a GET request to /USER_ID/skydrive/shared, where USER_ID is either me or the user ID of the consenting user. Then, if the item is included in the response, replace the photo ID with the item's ID in the code that follows.

public void createTag() {
	final String tagsPath = "file.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!131/tags";
	final LiveOperationListener opListener = new LiveOperationListener() {
		public void onError(LiveOperationException exception, LiveOperation operation) {
			   resultTextView.setText("Error creating tag: " + exception.getMessage());
		   }
		public void onComplete(LiveOperation operation) {
			JSONObject result = operation.getResult();
			String text = "Photo tagged:\nID = " + result.optString("id");
			   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("x", 0.5);
					body.put("y", 0.5);
					JSONObject user = new JSONObject();
					user.put("name", "Stig Struve-Christensen");
					user.put("id", "a6b2a7e8f2515e5e");
					body.put("user", user);
					   client.postAsync(tagsPath, body, opListener);    
				}
				catch(JSONException ex){
					resultTextView.setText("Error creating tag: " + ex.getMessage());
				}
			   }
		}
	);
}

Delete a tag

To delete a tag, use code like the this. Change the tag ID to the tag ID of the tag that you want to remove. The wl.skydrive_update scope is required.

Note

Only the owner of an item can delete that item's tags.

public void deleteTag() {
	String tagId = "tag.2e82e8a5445fe036.2e82e8a5445fe036!1111.47CinUEOE1qvXn3UqNHvV2EkAKg";
	client.deleteAsync(tagId , new LiveOperationListener() {
		public void onError(LiveOperationException exception, LiveOperation operation) {
			   resultTextView.setText("Error deleting tag: " + exception.getMessage());
		   }
		public void onComplete(LiveOperation operation) {
			resultTextView.setText("Tag deleted.");
		   }
	   });
}

Remarks

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