Android Detect Usb Storage for Kitkat (4.4)

Android detect usb storage for kitkat (4.4)

Please post code for your BroadcastReceiver and AndroidManifest.xml.

Manifest should look something like

<application>
<!--- ... --->
<receiver android:name=".UsbBroadcastReceiver"
android:exported="true"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<action android:name="android.intent.action.MEDIA_UNMOUNTED" />
<data android:scheme="file" />
</intent-filter>
</receiver>
</application>

Note the data tag, it's often overlooked.

In your onReceive implementation the URI to root of mount can be obtained from Intent#getData().

Also note that this will not work on Android 6.0 (M) and above. For 6.0 and above your code must request access to USB via one of two ways:

1) UsbManager#requestPermission

2) Intent#ACTION_OPEN_DOCUMENT_TREE as part of Storage Access Framework

Get access to USB mass storage device in android

In this example I am using the FileUtils from Apache, but event without it you will see the logic used to read a USB Flash drive:

private UsbManager usbManager;
private UsbDevice clef;
ArrayList<File> images;

usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
clef = null;

if (usbManager != null)
{
HashMap<String,UsbDevice> deviceList = usbManager.getDeviceList();
if (deviceList != null)
{
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while (deviceIterator.hasNext()) {
clef = deviceIterator.next();
}
}
}

if (clef != null)
{
File directory = new File("/storage/UsbDriveA/");
if (directory != null) {
if (directory.canRead()) {

images = new ArrayList<File>();
String[] imageExtensions = {"jpg","jpeg","png","gif","JPG","JPEG","PNG","GIF"};
Iterator<File> iterateImages = FileUtils.iterateFiles(directory, imageExtensions, true);
while (iterateImages.hasNext()) {
File theImage = iterateImages.next();
if (!theImage.getName().startsWith(".", 0))
images.add(theImage);
}

// custom process / methods... not very relevant here :
imageIndex = 0;
scale = 1.0f;
countImgs = images.size();
loadImage(imageIndex);
}
}
}

In my manifest I have those lines, although I'm not sure they're all mandatory...

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.usb.host" />
<uses-permission android:name="android.permission.USB_PERMISSION" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

How to read/write external USB storage on Android?

USB Drives get mounted to your device just like an SDCard does essentially*.

The mount path usually resides at:

/storage/usb0/

I have not used this on many devices other then my Droid running CyanogenMod, your device may very. You can smiply use a file manager to explore this path. The directories will still exist even if there is no mount path, so you will be able to determine the path.

Getting SDard(s) path and size in KitKat 4.4.2

You can find all shared storage devices by calling Context.getExternalFilesDirs() or Context.getExternalCacheDirs(). Then, you can call File.getUsableSpace() or use android.os.StatFs to check available disk space at each location.

These APIs are also available in ContextCompat in the support library to seamlessly work on pre-KitKat devices.

Using Android phone as USB Host to mount external drives to phone and accessing the memory of the external drive through phone

The only sure way of getting this done is to use API level above 12, otherwise a few phones may have support for usb host but most of them wont support it.
The reason being first of all you need hardware support for usb host, even if that is present the drivers needed might not be compiled into the kernel, i did some work while trying to implement usb host on nook color, even though it had hardware support, getting usb host working took almost 2 months and a dedicated app. So its not something you will be able to do for every device. A few might support it out of the box but even those would need root and lots more work for mounting drives and all that..
the DSLR camera also does it the same way look at the requirements

- Android device with ARMv7-A or newer CPU architecture (most 1ghz+ devices)
- Not rooted: Android 3.1 or higher with USB host kernel+API support
- Rooted: Android 2.3.1 or higher with USB host kernel support

These are the only devices that can support USB host.

Browsing a removable USB OTG on Android N, is Storage Access Framework really the only way?

Accessing removable storage data can be done without SAF in Android M and up. You just need to do low-level-ish coding. As illustrated by this project.
We in our project might opt for implementing something similar to this linked project since SAF is proving to be unreliable as of this time.

The project also includes an implementation of SAF but with the low-level USB reading library as a base.

Access and manipulate files and directories on an external USB storage in a rooted Android device

The external USB storage should be found in /storage/. There are USB drives there from A-F. As for choosing files, you might want to try AndroidFileBrowser. Here is a snippet of code that you could use to copy files:

public void copy(File src, File dst) throws IOException
{
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);

// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}


Related Topics



Leave a reply



Submit