DPReview.com is closing April 10th - Find out more

Help! Ant Android Studio gurus out thyere?

Started Jul 19, 2020 | Questions
DickyAus Veteran Member • Posts: 4,562
Help! Ant Android Studio gurus out thyere?

Oops, Ant should be Any.

When I go 'bush' for a photo trip I travel light. To secure my daily photos I use my Samsung A5 (2017), an Orico USB hub and some large capacity USB sticks. I have always used the built in facilities to handle the copying of files from SD Card to USB sticks. A clunky interface, but it works.

Since we are in lock down I thought I would amuse myself and write an App to automate the process and make life that bit easier.

Extensive searches of the web led me to try two approaches - USB Manager and Media Manager interfaces. Shown below is how far I got: (sorry about the poor quality of the photo)

I have two SD cards in my EM1.2 they show up as devices as shown above

Through USB manager I can see some info and through Media Manager I can see other things. But nowhere can I see everything I really want - I really need access to the USB devices file directory structures I can drill down through to select files and destinations for backups. Both managers return some sort of path or Directory file, but I can't find any way to use them. In the first photo the first four lines are derived from Media Manager and the last three from USB manager. The A5 is limited by Android 8 SDK 26.

To make matters worse I can't connect to the USB hub and to the computer to debug at the same time. Alas, with an AMD PC the emulator doesn't work either.

If anyone knows how the access USB media file structures by JAVA code I would really appreciate a few pointers.

Dicky.

PS. The first photo shows some numbers on the paper to the right of the phone. The lower number pair is my blood pressure when I started writing the App, the one above it shows what it was when I got Media Manager working, and the one above that is what is was when USB Manager was working.

 DickyAus's gear list:DickyAus's gear list
Olympus E-M1 Olympus E-M1 II Olympus M.Zuiko Digital ED 60mm F2.8 Macro Olympus M.Zuiko ED 75-300mm 1:4.8-6.7 II Olympus M.Zuiko 300mm F4 IS Pro +1 more
ANSWER:
This question has not been answered yet.
Olympus E-M1
If you believe there are incorrect tags, please send us this post using our feedback form.
Simon Garrett Veteran Member • Posts: 7,461
Re: Help! Ant Android Studio gurus out thyere?

DickyAus wrote:

Oops, Ant should be Any.

When I go 'bush' for a photo trip I travel light. To secure my daily photos I use my Samsung A5 (2017), an Orico USB hub and some large capacity USB sticks. I have always used the built in facilities to handle the copying of files from SD Card to USB sticks. A clunky interface, but it works.

Since we are in lock down I thought I would amuse myself and write an App to automate the process and make life that bit easier.

Extensive searches of the web led me to try two approaches - USB Manager and Media Manager interfaces. Shown below is how far I got: (sorry about the poor quality of the photo)

I have two SD cards in my EM1.2 they show up as devices as shown above

Through USB manager I can see some info and through Media Manager I can see other things. But nowhere can I see everything I really want - I really need access to the USB devices file directory structures I can drill down through to select files and destinations for backups. Both managers return some sort of path or Directory file, but I can't find any way to use them. In the first photo the first four lines are derived from Media Manager and the last three from USB manager. The A5 is limited by Android 8 SDK 26.

To make matters worse I can't connect to the USB hub and to the computer to debug at the same time. Alas, with an AMD PC the emulator doesn't work either.

If anyone knows how the access USB media file structures by JAVA code I would really appreciate a few pointers.

Dicky.

PS. The first photo shows some numbers on the paper to the right of the phone. The lower number pair is my blood pressure when I started writing the App, the one above it shows what it was when I got Media Manager working, and the one above that is what is was when USB Manager was working.

I've never needed to browse the file structure from an app, so I'm just guessing and Googling.  Does Storage Access Framework help?  I also found this on stackoverflow, which  gives a couple of links: https://developer.android.com/guide/topics/providers/document-provider.html .

-- hide signature --

Simon

 Simon Garrett's gear list:Simon Garrett's gear list
Nikon D800
OP DickyAus Veteran Member • Posts: 4,562
Re: Help! Ant Android Studio gurus out thyere?

Hi Simon, many thanks for the reply and suggestion. I did look at storage access framework but it seemed to be biased towards Android files and media.

I have had some success with this intent:

assert bSelect != null; // My select drive button
bSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent ii = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(ii, REQUEST_CHOOSE_DRIVE);
}
});

This opens up the clunky interface I was trying to avoid but it does return a DocumentFile Uri that I can use to drill down to anything on a USB drive or camera SD cards. I only need to use the intent to get Uri for the camera SD Cards I copy from and the two backup drives on the USB hub. So it doesn't cause any angst.

I found populating a Recyclerview with a lot of images caused the dreaded ANR error. So I shifted the slow bit to an async thread and it all works fine now.

The onActivityResult code:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CHOOSE_DRIVE) {
USB_Files = new ArrayList<>();
if (data != null) {
Uri uri = data.getData();
if (uri == null) {
tvSel.setText("No selection");
} else {
tvSel.setText("Selected: " + uri.getPath());
DocumentFile documentFile = DocumentFile.fromTreeUri(this, uri);
AsyncList getFiles = new AsyncList();
getFiles.execute(documentFile);

}
}
}
}

and the Async list:

public class AsyncList extends AsyncTask<DocumentFile, String, Boolean> {
private OnTaskCompleted listener;
private DocumentFile dof;

@Override
protected Boolean doInBackground(DocumentFile... dfs) {
DocumentFile df = dfs[0];
int i = 1;
int j = df.listFiles().length;
for (DocumentFile file : df.listFiles()) {
USB_File f = new USB_File();
String sType = "";
if (file.isDirectory()) {
sType = "Directory";
} else if (file.isFile()) {
sType = "File";
}

f.setUri(file.getName(), sType, file.getUri());
USB_Files.add(f);
publishProgress("Found: " + i++ + " of " + j);
}

return true;
}

@Override
protected void onPostExecute(Boolean b){
lmFiles = new LinearLayoutManager(cx);
rcvFiles.setLayoutManager(lmFiles);
mFilesAdapter = new FileAdapter(cx, USB_Files, onclickInterfaceFile);
rcvFiles.setAdapter(mFilesAdapter);
}
}

Unfortunately a lot of the USB OTG interfaces are manufacturer specific. It does look like OTG was a late inclusion in Android phones and and not well thought out. I suppose what I am doing is a bit out of the ordinary, but 'U' stands for universal doesn't it.

Regards,

Dicky.

 DickyAus's gear list:DickyAus's gear list
Olympus E-M1 Olympus E-M1 II Olympus M.Zuiko Digital ED 60mm F2.8 Macro Olympus M.Zuiko ED 75-300mm 1:4.8-6.7 II Olympus M.Zuiko 300mm F4 IS Pro +1 more
OP DickyAus Veteran Member • Posts: 4,562
Re: Help! Ant Android Studio gurus out thyere?

At Last! Got it all working

Try hard enough and long enough and you can do anything with Android. To get to where I needed to go involved finding my way through many layers of loosely coupled overlapping system interfaces, and like peeling an onion the more layers I peeled the more I wanted to cry. But I suppose Android does try to be all things to all men so we must wear the burden of hideous inefficiency. Thank heaven for the processing power of modern devices.

Dicky.

 DickyAus's gear list:DickyAus's gear list
Olympus E-M1 Olympus E-M1 II Olympus M.Zuiko Digital ED 60mm F2.8 Macro Olympus M.Zuiko ED 75-300mm 1:4.8-6.7 II Olympus M.Zuiko 300mm F4 IS Pro +1 more
Simon Garrett Veteran Member • Posts: 7,461
Re: Help! Ant Android Studio gurus out thyere?

DickyAus wrote:

At Last! Got it all working

Try hard enough and long enough and you can do anything with Android. To get to where I needed to go involved finding my way through many layers of loosely coupled overlapping system interfaces, and like peeling an onion the more layers I peeled the more I wanted to cry. But I suppose Android does try to be all things to all men so we must wear the burden of hideous inefficiency. Thank heaven for the processing power of modern devices.

Dicky.

Glad you got there!  As you said, I get the impression that OTG is not well supported, and not in a very standardised way.

I've developed a few apps but I'm by no means an expert.  I find the developer.android.com reference section sometimes opaque and incomplete, and the guides and samples rather sparse.  I often rely on Google (usually ending up at stackoverflow).  I have to hope that someone has had the same problem (and solved it) before.

Worse: you get something working only to find that that in the next release of Android some API is "deprecated" or simply doesn't work any more.  An example: I had an app that replicated the UK telephone Speaking Clock and it has a feature to set an alarm, and restart the app when the alarm fires.  Not with Android 10, where the wise Android Gods have severely reduced the functionality of alams, and you can no longer start activities from the background.  You can create a notification or play a sound, but not start an activity any more.

Anyway, glad you solved it.

-- hide signature --

Simon

 Simon Garrett's gear list:Simon Garrett's gear list
Nikon D800
Keyboard shortcuts:
FForum MMy threads