4

I am trying to create file dir to connected usb storage I have written following code to create dir :

public class MainActivity extends AppCompatActivity {

    private static final String ACTION_USB_PERMISSION = "com.example.usbtest.USB_PERMISSION";
    UsbManager mUsbManager;
    UsbDevice device;

    String s = null;
    TextView tvusb;

    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION_USB_PERMISSION.equals(action)) {
                synchronized (this) {
                    UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        if (device != null) {
                            Toast.makeText(getApplicationContext(), "permission allowed" + device, Toast.LENGTH_LONG).show();
                            //call method to set up device communication
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), "permission not allowed" + device, Toast.LENGTH_LONG).show();
                        Log.d("usb", "permission denied for device " + device);
                    }
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        tvusb = (TextView) findViewById(R.id.tvusb);

        mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);

        HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        while (deviceIterator.hasNext()) {
            device = deviceIterator.next();

            s = device.getDeviceName();

            int pid = device.getProductId();
            int vid = device.getVendorId();
            device = deviceList.get(s);

            tvusb.setText(s + "\n" + Integer.toString(pid) + "\n" + Integer.toString(vid));
        }


        PendingIntent mPermissionIntent;


        mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);

        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);


        registerReceiver(mUsbReceiver, filter);

        mUsbManager.requestPermission(device, mPermissionIntent);


        File fileContact = new File(s, "Contact");
        if (!fileContact.exists()) {
            fileContact.mkdirs();
        }

        if (!confirmDir(fileContact)) {

            Toast.makeText(getApplicationContext(), "Unable to create " + String.valueOf(fileContact), Toast.LENGTH_LONG).show();

        }

    }

    private static boolean confirmDir(File dir) {
        if (dir.isDirectory()) return true;  // already exists
        if (dir.exists()) return false;      // already exists, but is not a directory
        return dir.mkdirs();                 // create it
    }

}

manifest:

<manifest

    xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.usb"></uses-permission>

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

    <uses-feature android:name="android.hardware.usb.accessory"/>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <uses-library android:name="com.android.future.usb.accessory"/>


        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"/>
            </intent-filter>

            <meta-data
                android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
                android:resource="@xml/accessory_filter"/>

        </activity>

    </application>

</manifest>

I have also written - File fileContact = new File("/storage/emulated/0", "Contact");

But I am unable to create dir to usb storage.

How can I create dir to usb storage?

frogatto
  • 28,539
  • 11
  • 83
  • 129

1 Answers1

0

The way I understand your problem is that you want to write a file on the external storage. In this case, you should only need the *EXTERNAL* permissions. According to the documentation you'll need to:

  • check if the media is available and writable with something like:

    /* Checks if external storage is available for read and write */
    public boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;
    }
    

    (code verbatim from the source)

  • Then, you need to use getExternalStoragePublicDirectory() or getExternalFilesDir() depending on your files' policy (public or private to app).

For accessing the removable media, you should use the Storage Access Framework as outlined in this SO post with the ACTION_CREATE_DOCUMENT.

Community
  • 1
  • 1
Laur Ivan
  • 4,117
  • 3
  • 38
  • 62
  • i have connected pendrive through otg cable . so i want to create file on that pendrive. With this i am only access my internal memory storage not pendrive. –  Mar 21 '16 at 09:57