【问题标题】:Android - How to get file bytes from SdcardAndroid - 如何从 SD 卡获取文件字节
【发布时间】:2020-12-13 15:09:28
【问题描述】:

我正在尝试在 sdcard 中获取文档的一个字节。我所做的正是以下内容;

1-) 我选择文件。

   fun openFile() {
    val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
        addCategory(Intent.CATEGORY_OPENABLE)
        type = "application/pdf"
    }
    startActivityForResult(intent, PICK_PDF_FILE)
}

2-) 我现在有一个 URI。我正在尝试将其转换为文件。然后我尝试获取字节。

override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
    super.onActivityResult(requestCode, resultCode, resultData)

    if (requestCode == PICK_PDF_FILE && resultCode == Activity.RESULT_OK) {
        resultData?.data?.also { documentUri ->
            contentResolver.takePersistableUriPermission(
                documentUri,
                Intent.FLAG_GRANT_READ_URI_PERMISSION
            )
            var file = documentUri.toFile(); // I am trying to convert URI to FILE type. //ERROR LINE
            Log.d("test" , file.readBytes().toString()) // I'm trying to read the bytes.
        }
    }
}

但是这个错误:

 Caused by: java.lang.IllegalArgumentException: Uri lacks 'file' scheme: 
 content://com.android.externalstorage.documents/document/primary%3ADCIM%2Ftest.pdf

【问题讨论】:

    标签: java android android-studio kotlin illegalargumentexception


    【解决方案1】:

    考虑将文件加载为使用 TaskCompletionSource 异步加载的输入流:

    TaskCompletionSource<Stream> tcs = new TaskCompletionSource<>();
    

    在 openFile() 中:

    if (tcs == null || tcs.Task.isCompleted || tcs.Task.isCanceled)
    {
       startActivityForResult(Intent.createChooser(intent, "Select Document"), PICK_PDF_FILE);
       return tcs.getTask;
    }
    else
    {
       return tcs.getTask;
    }
    

    在 onActivityResult 中:

    if (requestCode == PICK_PDF_FILE && resultCode == Activity.RESULT_OK) {
       Uri uri = data.getData();
       tcs.setResult().getContentResolver().openInputStream(uri); 
    }
    else {
       tcs.setResult(null);
    }
    

    一旦有了 InputStream,就可以将 inputStream 转换为字节数组: Convert InputStream to byte array in Java

    但是,根据您需要对字节执行的操作,您可以直接使用流。例如,如果您要转换 PDF,可以使用 LEADTOOLS CloudServices 库: https://www.leadtools.com/support/forum/posts/t12369-

    【讨论】:

      【解决方案2】:
      File file = new File(path);
      int size = (int) file.length();
      byte[] bytes = new byte[size];
      try {
          BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
          buf.read(bytes, 0, bytes.length);
          buf.close();
      } catch (FileNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      

      来源:Android : How to read file in bytes?

      【讨论】:

        猜你喜欢
        • 2013-11-09
        • 1970-01-01
        • 2011-02-05
        • 2020-03-26
        • 2011-02-23
        • 2011-08-06
        • 2012-04-30
        • 2014-08-26
        • 2011-06-12
        相关资源
        最近更新 更多