![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Android™) Upload a File to an AI Provider (OpenAI, Google, Antropic, X)See more AI ExamplesUploads 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.Note: This example requires Chilkat v11.4.0 or greater.
// 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." } } |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.