0

I developed an android-app for a customer. He can:

  • pick multiple files (images, videos, files). These files are added to a ListView (containing only filenames).
  • start an upload of these files to a server later on.

Yet, i save the file-paths internally and whenever the upload-button is clicked, i pick the files from the saved paths and transmit them to the server.

The problem is that all my methods for getting the path, use depcreated methods and deprecated media-columns like f.i.

MediaStore.Video.Media.DATA

So i got to get rid of these and, as API10 recommends, don't work with absolute paths anymore.

I' m getting the filename like this:

Cursor cursor =  getApplicationContext().getContentResolver().query(uri, null, null, null, null);
int indexedname = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
cursor.moveToFirst();
String filename = cursor.getString(indexedname);
cursor.close();

QUESTION: So far so good. I got the file for the upload list. But how do i get the file only by name and without absolute path for the upload later on, whenever the user clicks on the upload-button?

I'm able to get a DocumentFile and the MIME from the ContentResolver and much more, but no File!

Thank you for your help in advance!

treesoft
  • 295
  • 2
  • 14
  • "But how do i get the file only by name and without absolute path for the upload later on, whenever the user clicks on the upload-button?" -- you don't. There is no file. – CommonsWare Jun 27 '20 at 10:59
  • Thank you @CommonsWare ! What would you do in my case? Somehow the user should be able to pick files and submit them. If it is not possible this way, how would you change the concept? "There is no file" what do you mean by that exactly. There is one, but i would say, i' m just not able to get it. Right? – treesoft Jun 27 '20 at 11:06
  • "Somehow the user should be able to pick files and submit them" -- use `ACTION_OPEN_DOCUMENT` to let the user pick content. Upload the content from the `Uri` that you get back. If by "later on" you mean being able to upload the content tomorrow, [use `takePersistableUriPermission()`](https://developer.android.com/reference/android/content/ContentResolver#takePersistableUriPermission(android.net.Uri,%20int)) to have long-term access to the content that the user chose. – CommonsWare Jun 27 '20 at 11:09
  • "There is one, but i would say, i' m just not able to get it. Right? " -- not necessarily. This is covered in [the first duplicate linked to above](https://stackoverflow.com/questions/56308559/create-a-file-from-a-photo-uri-on-android). – CommonsWare Jun 27 '20 at 11:11
  • Ok, thank you. I' m trying it the way you suggested. By "later on" i mean its just picked and not directly uploaded, just adding the filename in a list and in the next step, it has to be uploaded, like a minute later ;) – treesoft Jun 27 '20 at 11:25
  • If it is all in the same activity, you do not need to do anything special. If you are passing the `Uri` values between activities, even within your own app, that will get a bit complicated, and I recommend switching to a single activity and multiple fragments. – CommonsWare Jun 27 '20 at 11:26
  • `, i save the file-paths internally` It is unclear why you have some file paths instead of some uries if you let the user choose some files. Save those uries and use those uries -later- if you want to upload those files. – blackapps Jun 27 '20 at 12:32
  • @CommonsWare and blackapps : Thank you so much. I got it all working :) Another question, regarding another topic in my app: Is it possible to delete a vide/photo from gallery? If so: Can you give a hint? file.delete() is not sufficient...... – treesoft Jul 02 '20 at 21:13
  • @treesoft: I have not tried that, sorry. – CommonsWare Jul 02 '20 at 21:20
  • @CommonsWare No problem, thank you and have a nice weekend! – treesoft Jul 03 '20 at 11:31

0 Answers0