Example of Working with Files

To work with files, create the FileActivity application, which uses the methods implemented in the FileConnectorApi unit.

The application can be used to download files to a mobile device from file server or upload files to server. The application works identically to Yandex.Disk or Google.Drive.

Before creating the FileActivity application add a connection setting to the folder at filer server.

The FileActivity application includes one screen and buttons:

To view application work results, use the interactive example:

Tap any button

public class FileActivity extends AppCompatActivity {
   private static final String MY_URL = "http://testmasterfmp.fsight.cloud/";
   private static final VersionAPI MY_VERSION_API = VersionAPI.V_1;
   private static final String MY_ENVIRONMENT = "Leonid_environment";
   private static final String MY_PROJECT = "Leonid_project";
   private static final String MY_VERSION = "v1";
   private static final String MY_LOGIN = "Leonid";
   private static final String MY_PASSWORD = "123123";
   private static final String MOUNT_NAME = "nfs4";
   private static final String TAG = "FileActivity";
 
   private HyperHive hyperHive;
   private EditText editText;
   private TextView textView;
 
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_file);
       editText = findViewById(R.id.edit_text_file);
       editText.setText("123.txt");
       textView = findViewById(R.id.text_view_file);
 
       Handler uiHandler = new Handler();
 
       hyperHive = new HyperHiveState(getApplicationContext())
               .setHostWithSchema(MY_URL)
               .setApiVersion(MY_VERSION_API)
               .setEnvironmentSlug(MY_ENVIRONMENT)
               .setProjectSlug(MY_PROJECT)
               .setVersionProject(MY_VERSION)
               .setHandler(uiHandler)
               .buildHyperHive();
 
       authentication();
   }
 
   public void authentication() {
       boolean status = hyperHive.authAPI.auth(MY_LOGIN, MY_PASSWORD, true).execute().isOk();
       showStatus(status);
   }
 
   private void showStatus(boolean status) {
       Log.d(TAG, "status: " + status);
       Toast.makeText(this, String.valueOf(status), Toast.LENGTH_SHORT).show();
   }
 
   // Request to read and write
   private void getPermissions() {
       if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
               != PackageManager.PERMISSION_GRANTED) {
           ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
       }
   }
 
   // Create a file at the specified path: /storage/emulated/0/Download/
   private void createFile() {
       String fullPath = "/storage/emulated/0/Download/" + editText.getText().toString();
       getPermissions();
       File file = new File(fullPath);
       try {
           file.createNewFile();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }
 
   // Get file
   public void fileGet(View view) {
       createFile();
       boolean status = hyperHive.fileConnectorApi.fileGet("/storage/emulated/0/Download/" + editText.getText().toString(), editText.getText().toString(), MOUNT_NAME).execute().isOk();
       showStatus(status);
   }
 
   // Get metadata
   public void fileGetMeta(View view) {
       boolean status = hyperHive.fileConnectorApi.fileGetMeta(editText.getText().toString(), MOUNT_NAME).execute().isOk();
       showStatus(status);
   }
 
   // Send file
   public void filePut(View view) {
       getPermissions();
       boolean status = hyperHive.fileConnectorApi.filePut("/storage/emulated/0/Download/" + editText.getText().toString(), editText.getText().toString(), MOUNT_NAME).execute().isOk();
       showStatus(status);
   }
 
   // Delete file
   public void fileDelete(View view) {
       boolean status = hyperHive.fileConnectorApi.fileDelete(editText.getText().toString(), MOUNT_NAME).execute().isOk();
       showStatus(status);
   }
 
   // Get files list
   public void directoryGet(View view) {
       BaseStatus baseStatus = hyperHive.fileConnectorApi.directoryGet(".", MOUNT_NAME).execute();
       textView.setText(baseStatus.toString());
       showStatus(baseStatus.isOk());
   }
}

To encrypt files, use the application code:

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import com.mobrun.plugin.api.HyperHive
import ru.fsight.tutorial.app.Cache
 
class Main: Activity()
{
  override fun onCreate(savedInstanceState: Bundle?)
  {
    super.onCreate(savedInstanceState)
    button.setOnClickListener { text.text = fmpDownload() }
  }
   
  private fun fmpDownload(): String =
    try
    {
      val file = "lorem.txt"
      val file_local = "$cacheDir/$file"
      val mount = "nfs4"
      val password = "password"
      val response = hyperHive.filesApi.fileGet(file_local, file, mount, password).execute()
      val file_content = String(hyperHive.filesApi.fileDecrypt(file_local, password).execute())
      "$response\n\n$file_content"
    }
    catch (e: Exception)
    {
      "Error: $e."
    }
}
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.mobrun.plugin.api.HyperHive;
import com.mobrun.plugin.models.BaseStatus;
 
class Scratch extends Activity
{
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    ((Button) findViewById(R.id.button)).setOnClickListener((v) -> {
      ((TextView) findViewById(R.id.text)).setText(fmpDownload());
    });
  }
 
  private String fmpDownload()
  {
    try
    {
      final String file = "lorem.txt";
      final String file_local = getCacheDir() + "/" + file;
      final String mount = "nfs4";
      final String password = "password";
 
      HyperHive hyperHive;
      final BaseStatus response = hyperHive.filesApi.fileGet(file_local, file, mount, password).execute();
      final String file_content = new String(hyperHive.filesApi.fileDecrypt(file_local, password).execute());
      return response.toString() + "\n\n" + file_content;
    }
    catch (Exception e)
    {
      return "Error : " + e;
    }
  }
}

See also:

Examples of Android Framework Use