alexrintt / shared-storage

Flutter plugin to work with Android external storage.
http://alexrintt.io/shared-storage/
MIT License
54 stars 26 forks source link

filename is in correct when we create multiple instance of same file?[Android 9] #22

Closed dhaval-k-simformsolutions closed 1 year ago

dhaval-k-simformsolutions commented 2 years ago

Describe the bug The filenames are wrong when we save the same file multiple times in Android 9 version phones.

To Reproduce

  1. Grant document tree permission to any directory.
  2. Save the file using createFileAsString method in the storage
  3. save the Same file multiple times. like "myfile.csv"

Expected behavior When we save the same file multiple times it should create the files as below First time - myfile.csv Second time - myfile (1).csv Thrid time - myfile (2).csv

Note: It's working fine in Android 10 and above versions, But I have check-in 2 phones(Vivo and Asus) with android 9 and in those phones, it generates the above files as below which is a totally incorrect file format.

First time - myfile.csv Second time - myfile.csv (1) Thrid time - myfile.csv (2)

Screenshots Screenshot_20220413-142511

alexrintt commented 2 years ago

Are you using the mimeType param when creating the file with createFileAsString?

You should provide the mimeType param when providing the extension, the OS should handle the extension

createFileAsString(
  /// ...
  mineType: 'text/csv'
)
dhaval-k-simformsolutions commented 2 years ago

No, I am passing the mime type as well while createFileAsString. Please find the code below for the same.

      final newDocumentFile = await createFileAsString(
        selectedUriDir,
        mimeType: 'text/csv',
        content: csvData,
        displayName: fileName,
      );

I am using the package from the master branch as I was required to use exists(uri) function as I mentioned the issue #20

alexrintt commented 2 years ago

I see, then I need to search further through Android 9 and above. Thanks for the report

dhaval-k-simformsolutions commented 2 years ago

Yeah sure

dhaval-k-simformsolutions commented 2 years ago

@lakscastro As a temporary workaround of I am able to find the existing file with the same name using the findFile method and I just want to simply delete the older file and want to create the new one so every time there will be only one file will be there with the unique file name.

But the problem in the above workaround is to delete the file I required file path using which but there is no way using which I can get the file path of that file or If you provide method using which I can delete that file on persisted URI then it will work me as well.

Still, if there is a way to get the file path of persisted URI then it will be great. There is some method available in the native on StackOverflow as getRealPathFromUri I tried the same but got the below exception with that method.

getRealPathFromURI Exception : java.lang.UnsupportedOperationException: Unsupported Uri content://com.android.externalstorage.documents/tree/primary%3ADirectorName

alexrintt commented 2 years ago

is to delete the file I required file path

This is no longer available for recent Android APIs so this will not be implemented in this module (SAF)

But you can still use the delete method to delete a given Uri. Usage:

delete(uri); /// uri should be a Uri that you have write access
dhaval-k-simformsolutions commented 2 years ago

is to delete the file I required file path

This is no longer available for recent Android APIs so this will not be implemented in this module (SAF)

But you can still use the delete method to delete a given Uri. Usage:

delete(uri); /// uri should be a Uri that you have write access

delete method will delete the complete URI I guess means it will delete the complete directory my requirement is I want to just delete the file not directory

alexrintt commented 2 years ago

This is what you can do, take a look at example/ folder, we have 2 kind of Uris: Document (File) and Tree (Directory), you want to delete the Document Uri, the thing I don't know yet is how you can do it directly to you target DocumentUri

alexrintt commented 2 years ago

@dhaval-k-simformsolutions

Do you remember what data was here?

      final newDocumentFile = await createFileAsString(
        selectedUriDir,
        mimeType: 'text/csv',
        content: csvData,
        displayName: fileName, // here
      );

Do you remember if you were sending the displayName arg with the file extension? Like myfilename.png or just myfilename?

dhaval-k-simformsolutions commented 2 years ago

@dhaval-k-simformsolutions

Do you remember what data was here?

      final newDocumentFile = await createFileAsString(
        selectedUriDir,
        mimeType: 'text/csv',
        content: csvData,
        displayName: fileName, // here
      );

Do you remember if you were sending the displayName arg with the file extension? Like myfilename.png or just myfilename?

Hi @lakscastro
I was passing the filename along with the file extension.

alexrintt commented 2 years ago

Hey @dhaval-k-simformsolutions

Sorry for bothering you again, but can you please share some specs of your device? I'm not able to reproduce this bug neither on Android 11 (Xiaomi), Android 10 (Samsung) or Android 7.1 (Pixel)

Some useful info would be:

alexrintt commented 2 years ago

After a while I noticed this happens only to a few kind of mime types; and the csv is one of those. The fix is to use the complete mime type, in your case text/comma-separated-values

documentFile?.createFileAsString(
  mimeType: 'text/comma-separated-values', // Don't work with `text/csv`
  content: 'Sample File Content',
  displayName: 'Sample File',
);

Also, we need avoid sending the extension along with the displayName, so is recommended to use myfilename instead myfilename.png for example

dhaval-k-simformsolutions commented 2 years ago

Note: It's working fine in Android 10 and above versions, But I have check-in 2 phones(Vivo and Asus) with android 9 and in those phones, it generates the above files as below which is a totally incorrect file format.

@lakscastro

As I have already mentioned the device information in the issue, still mentioning the same again below. Note: It's working fine in Android 10 and above versions, But I have check-in 2 phones(Vivo and Asus) with android 9 and in those phones, it generates the above files as below which is the totally incorrect file format.

dhaval-k-simformsolutions commented 2 years ago

After a while I noticed this happens only to a few kind of mime types; and the csv is one of those. The fix is to use the complete mime type, in your case text/comma-separated-values

documentFile?.createFileAsString(
  mimeType: 'text/comma-separated-values', // Don't work with `text/csv`
  content: 'Sample File Content',
  displayName: 'Sample File',
);

Also, we need avoid sending the extension along with the displayName, so is recommended to use myfilename instead myfilename.png for example

Okay, I will try the same without extension with filename while creating the file and will let you know the updates.

dhaval-k-simformsolutions commented 2 years ago

After a while I noticed this happens only to a few kind of mime types; and the csv is one of those. The fix is to use the complete mime type, in your case text/comma-separated-values

documentFile?.createFileAsString(
  mimeType: 'text/comma-separated-values', // Don't work with `text/csv`
  content: 'Sample File Content',
  displayName: 'Sample File',
);

Also, we need avoid sending the extension along with the displayName, so is recommended to use myfilename instead myfilename.png for example

Okay, I will try the same without extension with filename while creating the file and will let you know the updates.

I have tried to createFileAsString with the only file name(without extension), and it creates a file as shown below which on openable. MicrosoftTeams-image (1)

dhaval-k-simformsolutions commented 2 years ago

@lakscastro Right now where I get the package from the master branch it gives me the below error while running the project.

e: /Users/manoj.padiya/.pub-cache/git/shared-storage-2ed784c56f885856e559378699df02736e9ee283/android/src/main/kotlin/io/lakscastro/sharedstorage/storageaccessframework/DocumentFileApi.kt: (10, 8): Unresolved reference: com
e: /Users/manoj.padiya/.pub-cache/git/shared-storage-2ed784c56f885856e559378699df02736e9ee283/android/src/main/kotlin/io/lakscastro/sharedstorage/storageaccessframework/DocumentFileApi.kt: (209, 41): Unresolved reference: child

Can you please check into it.

alexrintt commented 2 years ago

Merged #45 into master which fix this.

alexrintt commented 1 year ago

Closing as inactive.