【问题标题】:Choose a pdf from sdcard and convert it to byte array in android studio从 sdcard 中选择一个 pdf 并将其转换为 android studio 中的字节数组
【发布时间】:2018-01-20 22:36:02
【问题描述】:

我需要从 sdcard 中选择一个 pdf 并将其转换为字节数组。我不想表现出来。我搜索了很多,但这个问题没有答案。

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == SELECT_MAGAZINE_FILE && resultCode == RESULT_OK && data != null) {
        // Let's read picked image data - its URI
        Uri uri = data.getData();
        System.out.println(uri);
        System.out.println(uri.getPath());
        File file = new File(uri.getPath());
            //init array with file length
        byte[] bytesArray = new byte[(int) file.length()];

        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            fis.read(bytesArray); //read file into bytes[]
            fis.close();

            System.out.println(bytesArray);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

我收到了这个错误:

java.io.FileNotFoundException: /document/primary:myfile.pdf: open failed: ENOENT (No such file or directory)

【问题讨论】:

    标签: android arrays pdf android-intent


    【解决方案1】:

    充其量,只有当Uri 被传递到onActivityResult() 碰巧有file 方案时,您的代码才能工作。你的没有。它有一个content 方案。由于OutOfMemoryError,您的代码也将失败很多,因为您的代码无法分配byte[]。 PDF 文件可能会很大。

    因此,您的首要任务是找到其他解决方案,而不是将整个 PDF 文件读入 byte[],因为这将是不可靠的,您无法解决此问题,除非不这样做它。

    最终,要在Uri 标识的内容上获得InputStream,请使用ContentResolveropenInputStream()

    而且,从长远来看,您需要将此 I/O 移动到后台线程,因为现在您将在读取数据时冻结您的 UI。

    请注意,ContentResolver 用于 Uri 值,以及线程的使用,都包含在任何关于 Android 应用程序开发的体面书籍或课程中。

    【讨论】:

      【解决方案2】:

      感谢https://stackoverflow.com/users/115145/commonsware 的帮助。

      我将我的代码更改为这个并且它可以工作。最好用 AsyncTask 来做。

      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
      
          if (requestCode == SELECT_MAGAZINE_FILE && resultCode == RESULT_OK && data != null) {
              // Let's read picked image data - its URI
              Uri uri = data.getData();
              File file = new File(uri.getPath());
      
              try {
                  InputStream is = getActivity().getContentResolver().openInputStream(uri);
                  byte[] bytesArray = new byte[is.available()];
                  is.read(bytesArray);
      
                  //write to sdcard
                  /*
                  File myPdf=new File(Environment.getExternalStorageDirectory(), "myPdf.pdf");
                  FileOutputStream fos=new FileOutputStream(myPdf.getPath());
                  fos.write(bytesArray);
                  fos.close();*/
      
                  System.out.println(fileString);
              } catch (FileNotFoundException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      

      【讨论】:

        【解决方案3】:
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
        
            if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK) {
                Bundle extras = data.getExtras();
                Bitmap imageBitmap = (Bitmap) extras.get("data");
        
                Log.d("++++++++++", "++++ data log +++" + imageBitmap);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-04-14
          • 1970-01-01
          • 2011-05-16
          • 1970-01-01
          • 2020-05-29
          • 2018-05-06
          • 1970-01-01
          相关资源
          最近更新 更多