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:
FILEGET. Get file.
FILEGETMETA. Get file metainformation.
FILEPUT. Send file.
FILEDELETE. Delete file at server.
DIRECTORYGET. Get a list of specified directory files.
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();
}
//1. 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);
}
}
//2. Create a file at: /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();
}
}
//3. 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);
}
//4. Get metainformation
public void fileGetMeta(View view) {
boolean status = hyperHive.fileConnectorApi.fileGetMeta(editText.getText().toString(), MOUNT_NAME).execute().isOk();
showStatus(status);
}
//5. 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);
}
//6. Delete file
public void fileDelete(View view) {
boolean status = hyperHive.fileConnectorApi.fileDelete(editText.getText().toString(), MOUNT_NAME).execute().isOk();
showStatus(status);
}
//7. Get files list
public void directoryGet(View view) {
BaseStatus baseStatus = hyperHive.fileConnectorApi.directoryGet(".", MOUNT_NAME).execute();
textView.setText(baseStatus.toString());
showStatus(baseStatus.isOk());
}
}
See also: