0

In Android Studio using Pdf picker getting the path like this /document/document:15799 how to convert it to base64 or multipart or File. Or how I can get the absolute path of the pdf or docs.

private void selectPdf() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("application/pdf");
        intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, true);
        try {
            startActivityForResult(intent, PDF);

        } catch (ActivityNotFoundException e) {
            System.out.println("" + e);

        }
    }







@RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //Log.d(TAG, "onActivityResult: Call..");
      if (requestCode == PDF) {
                Uri pdfUri = data.getData();

                if (!pdfUri.equals("")) {
                    
                                try {
                                
                                pdfPath = pdfUri.getPath();
                                File file = new File(pdfPath);

                                String absolutePathPdf = file.getAbsolutePath();
                            }catch (Exception ae){
                                Log.e(TAG, "onActivityResult: "+ae.toString() );
                            }

    }
}
}
Ankur Shinde
  • 304
  • 4
  • 19
  • Check these answers - https://stackoverflow.com/questions/21601278/how-to-convert-a-file-to-base-64-like-pdf-txt-in-android, https://stackoverflow.com/questions/51528094/converting-file-into-string-then-into-base64-fromate-with-android-studio – Android Geek Mar 04 '22 at 11:48
  • java.io.FileNotFoundException: /document/document:15799: open failed: ENOENT (No such file or directory) getting this error. @AndroidGeek – Ankur Shinde Mar 04 '22 at 12:19
  • `In Android Studio using Pdf picker getting the path like this /document/document:15799` That is no file system path. That is part of a content scheme. Dont use pdfUri.getPath() but pdfUri.toString() to get full content scheme. You cannot use the File class with a content scheme. – blackapps Mar 04 '22 at 12:33
  • do we have any way to convert or get the absolute file path using content scheme? So I can convert that file to multipart. @blackapps – Ankur Shinde Mar 04 '22 at 12:46
  • 1
    You dont need an absolute file path as you can use the uri for it. Your problem has been reported many times and been solved. Google for multipartinputstream. – blackapps Mar 04 '22 at 13:25

1 Answers1

0

we can convert byte[] in base64 as well as in multipart too. below code works in android 11.

private void selectPdf() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("application/pdf");
        intent = Intent.createChooser(intent,"Choose a file");
        startActivityForResult(intent,REQ_PDF);
    }



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

if (requestCode == REQ_PDF && resultCode == RESULT_OK && data != null) {
            try {
                Uri pdfUri = data.getData();
                frontImagePath = getPDFPath(pdfUri);
                File file = new File(frontImagePath);
                fileSize = file.length();

            }catch (Exception e){
              //  Log.d(TAG, "Exception"+e);
            }
        }

}


 public String getPDFPath(Uri uri){
        String absolutePath = "";
        try{
            InputStream inputStream = getActivity().getContentResolver().openInputStream(uri);
            byte[] pdfInBytes = new byte[inputStream.available()];
            inputStream.read(pdfInBytes);
            String encodePdf = Base64.encodeToString(pdfInBytes, Base64.DEFAULT);
            Toast.makeText(getContext(), ""+encodePdf, Toast.LENGTH_SHORT).show();
            int offset = 0;
            int numRead = 0;
            while (offset < pdfInBytes.length && (numRead = inputStream.read(pdfInBytes, offset, pdfInBytes.length - offset)) >= 0) {
                offset += numRead;
            }

            String mPath = "";
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) {
                mPath= getActivity().getExternalFilesDir(Environment.DIRECTORY_DCIM) + "/" + Calendar.getInstance().getTime() + ".pdf";
            }
            else
            {
                mPath= Environment.getExternalStorageDirectory().toString() + "/" + Calendar.getInstance().getTime() + ".pdf";
            }

            File pdfFile = new File(mPath);
            OutputStream op = new FileOutputStream(pdfFile);
            op.write(pdfInBytes);

            absolutePath = pdfFile.getPath();

        }catch (Exception ae){
            ae.printStackTrace();
        }
        return absolutePath;
    }
Ankur Shinde
  • 304
  • 4
  • 19