Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Fix: Tflite files are not uploading in Model Management on Android Devices #373

Merged
merged 5 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions android/robot/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,6 @@

</activity>

<activity
android:name=".modelManagement.BackHandlingFilePickerActivity"
android:label="@string/app_name"
android:theme="@style/FilePickerTheme"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<service
android:name=".logging.SensorService"
android:enabled="true"
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.openbot.modelManagement;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.OpenableColumns;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -63,8 +66,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
Intent intent = result.getData();
// Handle the Intent
List<Uri> files = Utils.getSelectedFilesFromResult(intent);

String fileName = new File(files.get(0).getPath()).getName();
String fileName = getFileNameFromUri(files.get(0));
if (FileUtils.checkFileExistence(requireActivity(), fileName)) {
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
builder.setTitle(R.string.file_available_title);
Expand Down Expand Up @@ -110,6 +112,37 @@ public void handleOnBackPressed() {
requireActivity().getOnBackPressedDispatcher().addCallback(onBackPressedCallback);
}

/**
* Extracts the file name from a given Uri.
*
* @param uri The Uri from which to extract the file name.
* @return The extracted file name, or null if not found.
*/
private String getFileNameFromUri(Uri uri) {
String fileName = null;
if (uri.getScheme().equals("content")) {
// If the Uri uses the content:// scheme, use a ContentResolver to get the file name
ContentResolver contentResolver = requireActivity().getContentResolver();
Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
int displayNameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
if (displayNameIndex != -1) {
fileName = cursor.getString(displayNameIndex);
}
}
} finally {
cursor.close();
}
}
} else if (uri.getScheme().equals("file")) {
// If the Uri uses the file:// scheme, directly extract the file name
fileName = new File(uri.getPath()).getName();
}
return fileName;
}

private void processModelFromStorage(List<Uri> files, String fileName) {

Model item =
Expand Down Expand Up @@ -146,25 +179,11 @@ private void processModelFromStorage(List<Uri> files, String fileName) {
}

private void openPicker() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/octet-stream"); // Specify the MIME type for TFLite files
intent.addCategory(Intent.CATEGORY_OPENABLE);

Intent i = new Intent(requireActivity(), BackHandlingFilePickerActivity.class);
// This works if you defined the intent filter
// Intent i = new Intent(Intent.ACTION_GET_CONTENT);

// Set these depending on your use case. These are the defaults.
i.putExtra(BackHandlingFilePickerActivity.EXTRA_ALLOW_MULTIPLE, false);
i.putExtra(BackHandlingFilePickerActivity.EXTRA_ALLOW_CREATE_DIR, false);
i.putExtra(BackHandlingFilePickerActivity.EXTRA_MODE, BackHandlingFilePickerActivity.MODE_FILE);

// Configure initial directory by specifying a String.
// You could specify a String like "/storage/emulated/0/", but that can
// dangerous. Always use Android's API calls to get paths to the SD-card or
// internal memory.
i.putExtra(
BackHandlingFilePickerActivity.EXTRA_START_PATH,
Environment.getExternalStorageDirectory().getPath());

mStartForResult.launch(i);
mStartForResult.launch(intent);
}

@Nullable
Expand Down
Loading