4

我正在尝试创建文件目录到连接的 USB 存储我已经编写了以下代码来创建目录:

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

    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>

我也写过——File fileContact = new File("/storage/emulated/0", "Contact");

但我无法为 USB 存储创建目录。

如何创建到 USB 存储的目录?

4

1 回答 1

0

我理解您的问题的方式是您想在外部存储上写入文件。在这种情况下,您应该只需要*EXTERNAL*权限。根据文档,您需要:

  • 检查媒体是否可用和可写,例如:

    /* 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;
    }
    

    (源代码逐字逐句)

  • 然后,您需要使用getExternalStoragePublicDirectory()getExternalFilesDir()取决于文件的策略(对应用程序公开或私有)。

要访问可移动媒体,您应该使用此 SO 帖子中概述的存储访问框架和.ACTION_CREATE_DOCUMENT

于 2016-03-21T09:54:22.873 回答