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.