我正在尝试创建文件目录到连接的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存储创建目录。
如何创建dir到USB存储?
答案 0 :(得分:0)
我理解你的问题的方式是你想在外部存储上写一个文件。在这种情况下,您只需要*EXTERNAL*
权限。根据{{3}},您需要:
检查媒体是否可用且可写如下:
/* 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()
,具体取决于您的文件政策(公开或私有应用)。
要访问可移动媒体,您应使用the documentation中ACTION_CREATE_DOCUMENT
中列出的存储访问框架。