Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I didn't think this would be such a difficult task to solve but I can't seem to find the answer anywhere. I'm trying to get the file path to an image after a user navigates the folders on an android device and selects the file using . Such as:

/documents/imagename.png

But every solution I've tried results in a string similar to this

/documents/image:41879

First off "image:41879" is not the name of the selected file and the number after "image:" changes per picture, but as you can see this is not what I want. I'm looking to receive the full file path including the extension.

Would any one be able to help?

I have this code run when user clicks the "choose file" button

            @Override
            public void onClick(View v) {
                // ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
                // browser.
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*");
                startActivityForResult(intent, 10);
            }

And below is one of the many solutions I've tried which does not obtain the file path as I want

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

        TextView filename = (TextView) findViewById(R.id.filenameTextView);
        Uri uri = data.getData();
        String path = uri.getPath();
        directory = path;
        filename.setText(directory);
    }

Any and all help is greatly appreciated. Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
230 views
Welcome To Ask or Share your Answers For Others

1 Answer

If do you want to display the File Path to the user and it's like a core feature of your app, then I'm really sorry to say that, from API 29 or Android 10 we can't get the actual path of the file dur to some security reasons but if that's not the case then you can do it with

InputStream is = getContentResolver().openInputStream(data.getData());

Then you can get File Name and File Size using Cursor passing the Intent data and then using OpenableColumns.FILE_NAME and OpenableColumns.FILE_SIZE.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...