Android 12 解决NTFS格式U盘不能写的问题

问题描述

        在Android 12及更高版本上,Google引入了更为严格的存储权限和隐私控制。这可能导致一些外部设备(如U盘)在连接到Android设备上时无法进行写入操作。


原因分析:

  1. Scoped Storage和存储权限: Android 12引入了Scoped Storage,它对应用的文件访问进行了更严格的控制。如果应用没有正确请求和处理存储权限,它可能无法在外部设备上进行写入操作。确保你的应用已经请求了WRITE_EXTERNAL_STORAGE权限,并且根据Scoped Storage的规定进行了适配。

  2. 用户授权: 用户在连接U盘时可能需要授予应用写入U盘的权限。这通常通过系统的存储权限界面完成。确保你的应用引导用户在连接外部设备时正确授予所需的存储权限。

  3. 系统限制: 在一些情况下,Android系统可能对某些外部设备的写入权限进行了限制,尤其是对于NTFS格式的U盘。在这种情况下,你可能无法通过普通的应用层解决。系统的安全性和稳定性可能是Android 12对NTFS写入权限进行限制的原因之一。


解决方案:

        我们可以尝试使用

Android中读U文件可以通过Java实现,主要步骤包括检测U挂载、获取U路径、读文件等。以下是一个简单的示例代码,展示了如何在Android中读U文件: 1. **检测U挂载**: 需要通过广播接收器来检测U是否挂载。 2. **获取U路径**: 通过`Environment`类获取挂载的U路径。 3. **读文件**: 使用Java的`File`类和`IO`流来读文件。 ```java import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Environment; import android.widget.Toast; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class UsbFileManager { private Context context; public UsbFileManager(Context context) { this.context = context; registerUsbReceiver(); } private void registerUsbReceiver() { BroadcastReceiver usbReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)) { String usbPath = intent.getData().getPath(); Toast.makeText(context, "U已挂载: " + usbPath, Toast.LENGTH_SHORT).show(); } else if (intent.getAction().equals(Intent.ACTION_MEDIA_UNMOUNTED)) { Toast.makeText(context, "U已卸载", Toast.LENGTH_SHORT).show(); } } }; IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_MEDIA_MOUNTED); filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED); filter.addDataScheme("file"); context.registerReceiver(usbReceiver, filter); } public void writeToUsb(String fileName, String data) { File usbDir = getUsbDir(); if (usbDir != null) { File file = new File(usbDir, fileName); try (FileOutputStream fos = new FileOutputStream(file)) { fos.write(data.getBytes()); Toast.makeText(context, "写入U成功", Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); Toast.makeText(context, "写入U失败", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(context, "U未挂载", Toast.LENGTH_SHORT).show(); } } public String readFromUsb(String fileName) { File usbDir = getUsbDir(); if (usbDir != null) { File file = new File(usbDir, fileName); try (FileInputStream fis = new FileInputStream(file)) { byte[] buffer = new byte[fis.available()]; fis.read(buffer); return new String(buffer); } catch (IOException e) { e.printStackTrace(); Toast.makeText(context, "读取U失败", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(context, "U未挂载", Toast.LENGTH_SHORT).show(); } return null; } private File getUsbDir() { File[] externalStorageVolumes = ContextCompat.getExternalFilesDirs(context, null); for (File file : externalStorageVolumes) { if (file != null && file.getPath().contains("usbdrive")) { return file; } } return null; } } ``` ### 说明: 1. **检测U挂载**:通过注册广播接收器来检测U的挂载和卸载事件。 2. **获取U路径**:通过`getUsbDir`方法获取U路径。 3. **读文件**:使用`FileOutputStream`和`FileInputStream`进行文件的读操作。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jess.GJ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值