javaandroidpathandroid-fileprovider

how to get file path from content uri in android?


So my problem is that I want to get file path from content uri, I had googled but I didn't get any of the solutions, please help me out of this problem.

here is my code what I have tried

 Intent getPdf = getIntent();
        String action = getPdf.getAction();
        String type = getPdf.getType();
        Log.d("sdsfgsdfsdf",""+type);
        if (Intent.ACTION_SEND.equals(action) ||Intent.ACTION_VIEW.equals(action) || type != null){
            if (type.startsWith("application/pdf")){
                viewPdf(getPdf,action);
            }
        }

     private void viewPdf(Intent getPdf, String action) {
        Uri uri = Uri.parse(getPdf.getData().toString());

        if (uri != null) {
            File file = new File(String.valueOf(uri.getPath()));
            name = file.getName();
            path = String.valueOf(uri);
            Log.d("sdsfgsdfsdf",""+name);
        }else {
//            Toast.makeText(this, ""+ uri.toString(), Toast.LENGTH_SHORT).show();

        }
    }

This is the output content://com.android.externalstorage.documents/document/primary%3AWhatsApp%2FMedia%2FWhatsApp%20Documents%2FAJAX_And_PHP_Building_Responsive_Web_Applications-(2006).pdf

but I want to get file:// path instead of content uri


Solution

  • You can use PickiT library for get path from URI.

    Sample code for how to use it.

    PickiT pickiT = new PickiT(this, new PickiTCallbacks() {
            @Override
            public void PickiTonUriReturned() {
            }
    
            @Override
            public void PickiTonStartListener() {
            }
    
            @Override
            public void PickiTonProgressUpdate(int progress) {
            }
    
            @Override
            public void PickiTonCompleteListener(String filePath, boolean wasDriveFile, boolean wasUnknownProvider, boolean wasSuccessful, String Reason) {
                if (wasSuccessful) {
                    // Use file path
                }
            }
            @Override
            public void PickiTonMultipleCompleteListener(ArrayList<String> paths, boolean wasSuccessful, String Reason) {
    
            }
        }, this);
    
    pickiT.getPath(uri, Build.VERSION.SDK_INT);