1

With reference to the question asked in the same category, and as an extension of this:

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?)

My question:

In my HTC 816 Desire phone [Note: this device is upgraded to MarshMallow but still has crashes of KitKat. As a developer, I have experienced that Camera opened by intent to get the image captured by it, never releases its resources. This gives me an internal crash, when I try to take the second picture.], the returned path is starting with "/document/primary:", and it is retained in 'pdfUri.getPath()' resulting in file not found exception. Also when I search in 'MediaStore.Images.Media.DATA', column index returned is -1.

Q1. Should I simply remove "/document/primary:" from the file path, when the column index comes -1. As because in higher phones (23 and above), this (pdfUri.getPath()) works OK, giving me correct path.

Q2. Where should I get the patches for my phone to fix Camera resource not releasing bug, and there are other bugs at Firm-Ware level.

Please guide me, as well, by asking this question correctly, if in case it is not been asked accurately.

Abhinav Saxena
  • 1,990
  • 2
  • 24
  • 55
  • 1
    `"/document/primary:", and it is retained in 'pdfUri.getPath()'`. Yes but that is no file system path but part of a content scheme. Have a look at pdfUri.toString() to see complete content scheme. Use the pdfUri directly to handle the file. – greenapps Aug 24 '18 at 07:52
  • 1
    `how to get the file path of a file?`. No. You mean: 'how to get the file path from a content scheme?'. Well you dont. You adapt to modern times and use the content scheme (or uri) directly. – greenapps Aug 24 '18 at 07:55
  • @greenapps: I have edited the title of the target question from "how to get the file path of a file?" to "how to get the path from URI that points to PDF document". I hope it gets approved and is reflected soon. Also I know about content-schemes. I will try your suggestion: 'pdfUri.toString()' – Abhinav Saxena Aug 24 '18 at 09:03
  • 1
    YOU SHOULD NOT TRY TO GET A FILE PATH FROM AN URI – greenapps Aug 24 '18 at 09:05
  • @greenapps: Yes, true, retrieval from Content Resolver is recommended, but it is not working in KitKat device, in my case it is upgraded to MarshMallow, but the bugs that are OS specific are not resolved. – Abhinav Saxena Aug 24 '18 at 09:07

1 Answers1

1

In order to resolve the problem, I am trying to do the following:

  1. I am going to make the code generic, that I got as a solution, for many other file formats other than 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. In order to be able to release Camera resource, I will use surface view based Camera Activity which allows to do that. Here is a part of the code:

Release camera resource:

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

Happy Coding :-)

Abhinav Saxena
  • 1,990
  • 2
  • 24
  • 55