通过OTG连接线的Andr​​oid文件写入到USB [英] Android write files to USB via OTG cable

查看:340
本文介绍了通过OTG连接线的Andr​​oid文件写入到USB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找关于Android文件写入许多话题,但他们大部分想写文件到Android内部存储。谁想写外部SD卡上的文件别人没有成功可言。我的情况很相似,但我认为这将文件写入外部USB是一个完全不同的情况。

I've been searching for many topics about android file writing, yet most of them wanted to write files to android internal storage. Others who wanted to write files on external SD card didn't success at all. My case is quite similar but I think that writing files to external USB is a totally different case.

我使用三星Galaxy Note II运行的股票的TouchWiz 4.4.2 [不扎根。我的手机支持micro-USB-OTG,我可以挂载我的USB作为rwxrwx - X没有生根。我的USB的完整路径是/存储/ UsbDriveA。

I am using Samsung galaxy Note II running stock TouchWiz 4.4.2 [not rooted]. My phone supports micro-USB-OTG and I can mount my USB as rwxrwx--x without rooting. The complete path of my USB is /storage/UsbDriveA.

我试图用Environment.getExternalStorageDirectory()获得直接的路径,或使用路径(上述),但他们都没有成功。第一个返回内部存储路径,第二个返回权限被拒绝的错误。我已经把

I've tried to use Environment.getExternalStorageDirectory() to get the path or use the path (mentioned above) directly but neither of them succeed. The first one returns internal storage path and the second one returns an error with "permission denied". I have already put the

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

在Android清单,所以我不知道为什么我的code没有工作。

in Android Manifest so I wondered why my code didn't work.

此外,我可以用root浏览器(使用它没有根)和简单的浏览器写东西到我的USB因此,我相信,有一种方法可以做到这一点。

Moreover, I can write anything to my USB using Root Browser (use it without root) and Simple Browser thus I believe that there's a way to do that.

下面是我的code:

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 4.4中,可以使用存储访问架构访问到​​可移动介质(见的https://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html).
例如,我成功试图从本地存储的PDF文件复制到通过OTG适配器连接的可移动存储器。唯一的限制:用户必须选择目标文件夹

From android 4.4, you can use Storage Access Framework to access to removable media (see https://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html). For example, I tried with success to copy a pdf file from local memory to removable memory connected by OTG adapter. The only limitation: the user has to choose a destination folder.

1)调用Intent.ACTION_CREATE_DOCUMENT:

1) call 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)拦截返回意图

2) intercept the return intent

@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,并用它来复制文件

3) use the ContentResolver to open the outputStream and use it to copy the file

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();
        }
    }
}

这篇关于通过OTG连接线的Andr​​oid文件写入到USB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆