gpt4 book ai didi

android - 无法将文件写入 USB 存储

转载 作者:行者123 更新时间:2023-12-04 14:12:09 25 4
gpt4 key购买 nike

我正在尝试创建文件目录到连接的 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 存储的目录?

最佳答案

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

  • 检查媒体是否可用和可写,例如:
    /* 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()取决于您的文件政策(对应用程序公开或私有(private))。

  • 要访问可移动媒体,您应该使用 this SO post 中概述的存储访问框架。与 ACTION_CREATE_DOCUMENT .

    关于android - 无法将文件写入 USB 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36126913/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com