Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Alternatively, you can use the generic APSCloud.sendRequest() method to make REST calls directly against the Cloud APIs. For more information, see Making Generic REST API Calls.

Note

All Cloud API calls must be made on the UI (main) thread, and callbacks are executed on the UI thread.

...

The following example uploads a file from the device (/res/raw/reference.pdf) to the MBS storage server. Since the method call requires that uploaded data be an instance of java.io.File, the application needs to copy the resource to a read-write directory before uploading it. Locally storing the file requires that the WRITE_EXTERNAL_STORAGE permission is included in your AndroidManifest.xml file.

The progress callback calls the setProgress() method on a ProgressBar object, displaying the status of the upload. After the request completes, the application displays a toast notification.

Code Block
languagejava
collapsetrue
HashMap<String, Object> data = new HashMap<String, Object>();
String filename = "reference.pdf";

// Need to copy the resource to a read-write directory to upload it
if (!createExternalStoragePrivateFile(R.raw.reference, filename)) return;

File file = new File(currentActivity.getExternalFilesDir(null), filename);
data.put("file", file);
data.put("name", "Reference Manual");

try {
    APSFiles.create(data, new APSClient.APSResponseHandler() {
        @Override
        public void onResponse(final APSResponse e) {
            if (e.getSuccess()) {
                APSCloud.log("PUSH", "Successfully subscribed to push!");
                progressBar.setVisibility(View.GONE);
                Toast.makeText(currentActivity, "File uploaded!", Toast.LENGTH_SHORT).show();
            }
            else {
                Log.e("UPLOAD", e.getMessage());
            }
        }
    },
    new APSClient.APSProgressHandler() {
        @Override
        public void onProgress(final int percentProgress, final boolean upload) {
            if (currentActivity != null) {
                progressBar.setProgress(percentProgress);
        }
    });
} catch (APSClientError e) {
    Log.e("UPLOAD", e.getMessage());
}

// Helper function to copy a resource to external storage, modified from:
// httphttps://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)

public static boolean createExternalStoragePrivateFile(int inputResource, String filename) {
    File file = new File(currentActivity.getExternalFilesDir(null), filename);
    try {
        InputStream is = currentActivity.getResources().openRawResource(inputResource);
        OutputStream os = new FileOutputStream(file);
        byte[] data = new byte[is.available()];
        is.read(data);
        os.write(data);
        is.close();
        os.close();
        return true;
    } catch (IOException e) {
        Log.w("ExternalStorage", "Error writing " + file, e);
        return false;
    }
}

...

Adding the APSCloudPush Library

...