代码之家  ›  专栏  ›  技术社区  ›  Abhinav Saxena

如何在onActivityResult回调中从PDF chooser intent library获取pdfUri文件路径?

  •  1
  • Abhinav Saxena  · 技术社区  · 7 年前

    关于同一类别中提出的问题,作为这一问题的延伸:

    How to get the file path from a URI that points to a PDF document?

    (How to get the file path from a URI that points to a PDF document?)
    

    我的问题是:

    "/document/primary:" ,并保留在 'pdfUri.getPath()' 导致文件找不到异常。当我搜索的时候 'MediaStore.Images.Media.DATA'

    第一季度。我应该简单地删除 “/document/primary:” 从文件路径,当列索引为-1时。因为在更高级的电话(23及以上)中,这个(pdfUri.getPath())工作正常,提供了正确的路径。

    问题2。我应该在哪里得到我的手机修补程序 Camera resource not releasing bug,在固件级别还有其他bug。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Abhinav Saxena    7 年前

    为了解决问题,我正在尝试执行以下操作:

    1. 我将使代码通用,我得到的解决方案,对于许多其他文件格式,而不是PDF:

      private String saveFileFromUri(Uri pdfUri){
          try{
              InputStream is=
              baseActivity.getContentResolver().openInputStream(pdfUri);
              byte[]bytesArray=new byte[is.available()];
              int read=is.read(bytesArray);
          //write to sdcard
      
          File dir=new File(Environment.getExternalStorageDirectory(),"/PRJ");
          boolean mkdirs=dir.mkdirs();
          File myPdf=new 
          File(Environment.getExternalStorageDirectory(),"/PRJ/myPdf.pdf");
          if(read==-1&&mkdirs){
      
          }
          FileOutputStream fos=new FileOutputStream(myPdf.getPath());
          fos.write(bytesArray);
          fos.close();
          //            System.out.println(fileString);
          return myPdf.getPath();
      }catch(FileNotFoundException e){
          e.printStackTrace();
      }catch(IOException e){
          e.printStackTrace();
      }
      return null;}
      
    2. 为了能够释放相机资源,我将使用基于表面视图的相机活动,允许这样做。下面是代码的一部分:

    释放相机资源:

        @Override
    public void onPause() 
    {
        super.onPause();
    
            if (mCamera != null){
                //              mCamera.setPreviewCallback(null);
                mPreview.getHolder().removeCallback(mPreview);
                releaseMediaRecorder();
                mCamera.release();        // release the camera for other applications
                mCamera = null;
    
            }
        }
    
        @Override
        public void onResume() {
            super.onResume();
            if (mCamera == null) {
                mCamera=getCameraInstance();
                mPreview = new CameraPreview(this.getActivity(), mCamera);
                preview.addView(mPreview);
            }
        }