Android™
Android™
Upload a File to an AI Provider (OpenAI, Google, Antropic, X)
See more AI Examples
Uploads a file to an AI provider using the provider's File API and returns theid that can later be used to reference the file in an query. This currently works with ChatGPT, Gemini, Claude, and Grok.
Chilkat Android™ Downloads
// 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);
boolean success = false;
CkAi ai = new CkAi();
// The provider can be "openai", "google", "claude", "grok", or any AI that supports uploading files for later reference by ID.
ai.put_Provider("openai");
// Use your provider's API key.
ai.put_ApiKey("MY_API_KEY");
// We can upload directly from a file in the filesystem, or from a Chilkat StringBuilder, or from a Chilkat BinData.
// Some AI providers require a content-type.
// Also, some AI providers are picky about what content-type's are accepted.
// Check the AI provider's documentation.
// "application/json" is generally always acceptable.
// "text/plain" can be used as a fallback for any text file.
String contentType = "application/json";
String localFilePath = "qa_data/hamlet.json";
String filenameOnServer = "hamlet.json";
// Upload directly from a file.
String file_id = ai.uploadFile(localFilePath,contentType);
if (ai.get_LastMethodSuccess() == false) {
Log.i(TAG, ai.lastErrorText());
Log.i(TAG, "AI File Upload Failed.");
}
else {
Log.i(TAG, "File ID: " + file_id);
Log.i(TAG, "File uploaded.");
}
// Upload from the contents of a StringBuilder
CkStringBuilder sb = new CkStringBuilder();
success = sb.LoadFile(localFilePath,"utf-8");
if (success == false) {
Log.i(TAG, sb.lastErrorText());
return;
}
file_id = ai.uploadFileSb(sb,filenameOnServer,contentType);
if (ai.get_LastMethodSuccess() == false) {
Log.i(TAG, ai.lastErrorText());
Log.i(TAG, "AI File Upload Failed.");
}
else {
Log.i(TAG, "File ID: " + file_id);
Log.i(TAG, "File uploaded.");
}
// Upload from the contents of a BinData
CkBinData bd = new CkBinData();
success = bd.LoadFile(localFilePath);
if (success == false) {
Log.i(TAG, bd.lastErrorText());
return;
}
file_id = ai.uploadFileBd(bd,filenameOnServer,contentType);
if (ai.get_LastMethodSuccess() == false) {
Log.i(TAG, ai.lastErrorText());
Log.i(TAG, "AI File Upload Failed.");
}
else {
Log.i(TAG, "File ID: " + file_id);
Log.i(TAG, "File uploaded.");
}
}
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."
}
}