Home > front end >  how to get file path from content uri in android?
how to get file path from content uri in android?

Time:01-18

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:WhatsApp/Media/WhatsApp Documents/AJAX_And_PHP_Building_Responsive_Web_Applications-(2006).pdf

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

CodePudding user response:

It is unclear where you got that uri that content scheme from.

You should have started with that

Further it makes no sense trying to get a path for it as mostly you will not have read access then.

Further you did not tell why you want a path

Use the uri! Use the content scheme.

CodePudding user response:

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);
  • Related