【问题标题】:Android write files to USB via OTG cableAndroid 通过 OTG 线将文件写入 USB
【发布时间】:2015-02-27 07:52:24
【问题描述】:

我一直在寻找有关android文件写入的许多主题,但大多数人都想将文件写入android内部存储。其他想在外部SD卡上写入文件的人根本没有成功。我的情况非常相似,但我认为将文件写入外部 USB 是完全不同的情况。

我正在使用三星 Galaxy Note II 运行库存 TouchWiz 4.4.2 [未植根]。我的手机支持 micro-USB-OTG,我可以将我的 USB 挂载为 rwxrwx--x 而无需生根。我的 USB 的完整路径是 /storage/UsbDriveA。

我尝试使用 Environment.getExternalStorageDirectory() 来获取路径或直接使用路径(如上所述),但它们都没有成功。第一个返回内部存储路径,第二个返回“权限被拒绝”错误。我已经放了

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

在 Android Manifest 中,所以我想知道为什么我的代码不起作用。

此外,我可以使用 Root Browser(无需 root 即可使用)和 Simple Browser 将任何内容写入我的 USB,因此我相信有办法做到这一点。

这是我的代码:

File file = new File(path.getAbsolutePath(), "test.txt");
// File file = new File("/storage/extSdCard","test.txt");
err = false;
  try {
    FileOutputStream f = new FileOutputStream(file);
    PrintWriter pw = new PrintWriter(f);
    pw.print(get);
    pw.flush();
    pw.close();
    f.close();
  }catch (Exception e) {
    e.printStackTrace();
    Toast.makeText(MainActivity.this, "writing error",Toast.LENGTH_LONG).show();
                err = true;
   }
   Log.i("File Path:", file.getPath());

提前致谢!!!

【问题讨论】:

    标签: android android-external-storage writing


    【解决方案1】:

    从 android 4.4 开始,您可以使用 Storage Access Framework 访问可移动媒体(请参阅https://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html)。 例如,我尝试成功地将 pdf 文件从本地内存复制到通过 OTG 适配器连接的可移动内存。唯一的限制:用户必须选择一个目标文件夹。

    1) 调用 Intent.ACTION_CREATE_DOCUMENT:

    Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
    intent.setType("application/pdf");
    intent.putExtra(Intent.EXTRA_TITLE, file.getName());
    startActivityForResult(intent, REQUEST_CODE);
    

    2) 拦截返回意图

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if(requestCode == REQUEST_CODE) {
            if (resultCode != RESULT_OK) return;
            copyFile(fileToCopy, data.getData());
        }
    }
    

    3) 使用 ContentResolver 打开 outputStream 并使用它来复制文件

    private void copyFile(File src, Uri destUri) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
    
        try {
            bis = new BufferedInputStream(new FileInputStream(src));
            bos = new BufferedOutputStream(getContentResolver().openOutputStream(destUri));
            byte[] buf = new byte[1024];
            bis.read(buf);
            do {
                bos.write(buf);
            } while(bis.read(buf) != -1);
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null) bis.close();
                if (bos != null) bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    【讨论】:

    • 我试过这个,它导致了一个错误Caused by: java.lang.IllegalArgumentException: Invalid URI: content://com.android.externalstorage.documents/document/DCD2-B740%3Afake_title.pdf
    【解决方案2】:

    来自https://source.android.com/devices/storage/

    从 Android 4.4 开始,...

    WRITE_EXTERNAL_STORAGE 权限只能授予对 设备上的主要外部存储。不得允许应用程序 写入辅助外部存储设备,除了它们的 合成权限允许的特定于包的目录。 以这种方式限制写入可确保系统可以清理文件 卸载应用程序时。

    因此,从具有多个外部存储设备的 Android 4.4 开始,您将只能在主外部存储设备上进行写入。考虑到外部存储不仅仅意味着“真正的外部”设备。定义如下(来自External Storage reference

    外部存储可以通过物理媒体(例如 SD 卡),或通过暴露的内部存储的一部分通过 仿真层。

    无论如何,有一种解决方法可以使用媒体内容提供程序写入辅助外部存储。看看http://forum.xda-developers.com/showthread.php?t=2634840

    我在我的一个项目中使用过它,但正如作者所说,它远非理想的解决方案,并且不能保证在即将到来的 Android 版本上工作,所以你不能让你的所有应用程序依赖它解决方法。

    【讨论】:

    • 我已经尝试使用你上面提到的class(xda),但它需要android.permission.WRITE_MEDIA_STORAGE,它只能在系统应用程序中应用。我不想将应用程序放在 /system 中,因为它毫无意义。所以我想知道是否有其他方法可以做到这一点?无论如何,非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2013-03-12
    • 2021-03-08
    • 2017-05-27
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多