HBiSoft / PickiT

An Android library that returns real paths from Uri's

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

File not found exception when picking file from downloads

saurabhsbora opened this issue · comments

Whenever I select any file from downloads with filename containing spaces, it is giving me an error file not found exception & also a toast in a different language. Please provide a fix for this.

I have the traced the issue - it is due to invalid characters in the filename and we need to replace them with an underscore.

Android -10

What is the path that gets returned and what is the file name?

Please provide a screenshot using the demo application.

@HBiSoft I cannot capture the screenshot using the demo application as the downloads folder is disabled.

the downloads folder is disabled

I tested and I was able to select a file from the downloads folder.

Anyway, what is the file path that gets returned?

Anyway, what is the file path that gets returned?

@HBiSoft
Returned path by PickiT

E/RETURNED -: /storage/emulated/0/Download/Screenshot (94).png
This path is not found when you try to create a file object as the actual file name is screenshot__94_.png

You can reproduce the issue by renaming the file with spaces or brackets and then check if the file exists using File Object.
We need to explicitly replace those characters with an underscore and it works.

I tested this by selecting a file with the following name - VID 2019__1234_.mp4
PickiT returned the following path - /storage/emulated/0/Download/VID 2019__1234_.mp4

I tested this:

File mFile = new File(pathReturnedByPickiT);
if (mFile.exist){
    Log.e("File", "was found");
}

and the log gets called.


Can you please provide me with the Uri that gets returned in onActivityResult - data.getData();?

Can you please provide me with the Uri that gets returned in onActivityResult - data.getData();?
@HBiSoft
Uri - content://com.android.providers.downloads.documents/document/475

image
image

If i download an zip and extracted it in Download folder
Download
|__Extracted
|_File1
|_File2

In this case if i selected File1

It returns /storage/emulated/0/Download/File1

It should have returned /storage/emulated/0/Download/Extracted/File1

@enthussb
Thats very strange because I'm not replacing characters in the file name.
Please try the following and see what name gets returned:

@SuppressWarnings("TryFinallyCanBeTryWithResources")
private static String getFilePath(Context context, Uri uri) {
    Cursor cursor = null;
    final String[] projection = {MediaStore.Files.FileColumns.DISPLAY_NAME};
    try {
        cursor = context.getContentResolver().query(uri, projection, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            final int index = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DISPLAY_NAME);
            return cursor.getString(index);
        }
    }catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

@sugatmankar
Are you using the latest version? I remember fixing this in an earlier release.
If you are using the latest version, please open a new issue and provide as much info as possible.
Thanks.

@enthussb
That's very strange because I'm not replacing characters in the file name.
Please try the following and see what name gets returned:

@HBiSoft
This is the name returned by that function -
image
The original name in the file manager -
Screenshot_20200812-114146

Currently, I am facing this issue only when I pick any file from downloads.
To reproduce this issue please pick any file having spaces or other reserved characters in filename from the downloads folder when you open the intent and not by going through file manager -> downloads -> file

@enthussb
I'm not able to reproduce this.
Please provide me your entire Intent, so that I can test it as you have it.

@HBiSoft

Please provide me your entire Intent, so that I can test it as you have it.

final String[] ACCEPT_MIME_TYPES = {
                "application/pdf",
                "image/*"
        };
        Intent intent = new Intent()
                .setType("*/*")
                .setAction(Intent.ACTION_OPEN_DOCUMENT)
                .addCategory(Intent.CATEGORY_OPENABLE)
                .putExtra(Intent.EXTRA_MIME_TYPES, ACCEPT_MIME_TYPES);
        startActivityForResult(intent, REQUEST_SELECT_FILE);

I tested once more and I still was not able to reproduce this.
I'm not sure if the following will give the correct name, but it's worth the try, please let me know if the correct name is returned:

@SuppressWarnings("TryFinallyCanBeTryWithResources")
private static String getFilePath(Context context, Uri uri) {
    Cursor cursor = null;
    try {
        cursor = context.getContentResolver().query(uri, null, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            final int index = cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME);
            return cursor.getString(index);
        }
    }catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

@HBiSoft
I tested with the above code and still, I got the same name as the display name whereas the original name should have spaces replaced with underscores, or else it will give file not found exceptions.
image

Can you please explain what you meant with the following:

I cannot capture the screenshot using the demo application as the downloads folder is disabled.

Also, you said the following:

from the downloads folder when you open the intent and not by going through file manager -> downloads -> file

I assume you meant that after launching the intent -> Files - Downloads - screenshot__94_.png?

@HBiSoft

Can you please explain what you meant with the following:

It is the same as #2 Whenever I try to pick a file from downloads using PickIt I am unable to do that as all the files are disabled there.

I assume you meant that after launching the intent -> Files - Downloads - screenshot__94_.png?

There are two ways to go to downloads -

  1. As shown in the below picture
  2. By manually traversing through Internal Storage -> Downloads (File manager basically)

Whenever I pick a file having spaces or bracket in its name from downloads using the first method I am getting File not found exception.

Hope this will make it clear.

Screenshot_20200813-105839

@enthussb

It is the same as #2 Whenever I try to pick a file from downloads using PickIt I am unable to do that as all the files are disabled there.

No, video files are not disabled. In the demo application intent.setType("video/*"); is passed to the Intent.
I wanted you to test with the demo application (by selecting a video) to see if the problem might be with the Intent.
Instead of selecting an image, rename a video inside your downloads folder to a similar name and test it again.

Hope this will make it clear.

Yes, I tested both ways.

@HBiSoft The issue is if you rename it from your file manager it will work as it corrects the file name. Could you provide me your email so that I can send you the image there and then after downloading it you can check the issue?

@enthussb

The issue is if you rename it from your file manager it will work as it corrects the file name.

No, the issue is that an incorrect name is given when querying the DISPLAY_NAME column (this is not related to the library) and we will not be able to fix it afterwards (by renaming it) because how will we know that the name is incorrect..


I wanted you to test with the demo application (by selecting a video) to see if the problem might be with the Intent.
Instead of selecting an image, rename a video inside your downloads folder to a similar name and test it again.

Did you test this?


Could you provide me your email so that I can send you the image there and then after downloading it you can check the issue?

Sure here is my email - contact.hbisoft@gmail.com

PS. I did rename an image to screenshot__94_.png and selected if from the Downloads folder as you mentioned in #25 (comment)

@HBiSoft I checked with what you said and it works as file is renamed using the file manager but the problem which I am facing is different & I am unable to explain to you right now. Closing this temporarily