Android Question File.size with URI

Status
Not open for further replies.

Nokia

Active Member
Licensed User
Longtime User
Does file.size work with a URI?

if not how do I get the file size?
 

drgottjr

Expert
Licensed User
Longtime User
i think this has been answered numerous times by erel. you can't assume the uri points to a file (and even if it did, that you would be allowed to ask about it).
uri 1 and 2
people have tried to pose the question in different ways, but the answer seems to be the same.
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
i think this has been answered numerous times by erel. you can't assume the uri points to a file (and even if it did, that you would be allowed to ask about it).
uri 1 and 2
people have tried to pose the question in different ways, but the answer seems to be the same.

I see.. move the file so you can use file.exists or file.size..

how do you get the file name so you can copy?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
is this what you're talking about: sharing ? somebody makes a resource available to your app. the os tells you where you can find it. your app needs the other app to share its resource(s) with you. the other app makes the name known to any other app listed as an app that can handle a particular type of resource (eg, image). your app accepts the shared resource and asks the os where it is. take the pictures gallery. select an image. you see several icons, among them a sharing icon. tap that icon and you see a list of app that can accept an image. someday that list will include your app (when you create it). if the user clicks on your app, your app runs. it sees the name of the resource and its location (hint: not in the gallery). your app can now do its thing. the os oversees the relocation of the resource from the other app's territory to some neutral area where your app is allowed to copy it into your territory. for whatever reason (or lack of any reason), your app can't know how big the resource is in advance. maybe it forgot that step. seems pretty obvious to me that it's a piece of information that might be useful, but we're just visiting.
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
is this what you're talking about: sharing ? somebody makes a resource available to your app. the os tells you where you can find it. your app needs the other app to share its resource(s) with you. the other app makes the name known to any other app listed as an app that can handle a particular type of resource (eg, image). your app accepts the shared resource and asks the os where it is. take the pictures gallery. select an image. you see several icons, among them a sharing icon. tap that icon and you see a list of app that can accept an image. someday that list will include your app (when you create it). if the user clicks on your app, your app runs. it sees the name of the resource and its location (hint: not in the gallery). your app can now do its thing. the os oversees the relocation of the resource from the other app's territory to some neutral area where your app is allowed to copy it into your territory. for whatever reason (or lack of any reason), your app can't know how big the resource is in advance. maybe it forgot that step. seems pretty obvious to me that it's a piece of information that might be useful, but we're just visiting.

having an app that shares an image to mines would be a nice touch and may be later down the line. but that's not what I am needing.. I am needing to get the file name from the uri as When I upload the file it needs to have the same name as started out with and not some temp name I give it..
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
sorry, you lost me:
When I upload the file it needs to have the same name as started out with and not some temp name I give it..
when you upload the file? you never said anything about uploading.
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
I also answered this multiple times, you don't need to check. The resource is there.

I have copied the file to external drive so I can get the size of the file with file.size.. This also helps with me not having to change alot of my coding.

However I need to get original file name, is there a way to get that?
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
how would I try some of these java codes on the internet in my app?

link to code below

B4X:
#if java
public String getFileName(Uri uri) {
  String result = null;
  if (uri.getScheme().equals("content")) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    try {
      if (cursor != null && cursor.moveToFirst()) {
        result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
      }
    } finally {
      cursor.close();
    }
  }
  if (result == null) {
    result = uri.getPath();
    int cut = result.lastIndexOf('/');
    if (cut != -1) {
      result = result.substring(cut + 1);
    }
  }
  return result;
}
# end if

code to get full path...

and what about this code:
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
The official specification for URI's does NOT require them to include filenames, so your app can't assume there is one.

If however, your app will *ONLY* be used with a particular source/app that does *always* includes a filename in the URI, then you should be able to parse out the last part of the URI to get the filename. But this assumes that your app will *only* be used with a source that always includes a filename in the URI, otherwise it will fail.
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
The official specification for URI's does NOT require them to include filenames, so your app can't assume there is one.

If however, your app will *ONLY* be used with a particular source/app that does *always* includes a filename in the URI, then you should be able to parse out the last part of the URI to get the filename. But this assumes that your app will *only* be used with a source that always includes a filename in the URI, otherwise it will fail.


I am using content chooser witch does not always return uri with name in it. so If I take a pic with camera and I click on my app to get the images, it says file not found, but If I click the image in gallery and lets say share it to my email client,, the mail client has the correct file name...

why are we not able to get the file name?
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
Your assumption that the remote resource actually comes from a local file is wrong.

You can use this code to get the file name in some of the cases: https://www.b4x.com/android/forum/t...s-returned-from-contentchooser.39313/#content

I understand some of them are not actual files, some may be in a database or internal storage or some other format. what I'm saying is if it's in a database they still have a file name associated with it don't they?

how else are all of these after market file browsers getting names of file in different storage locations?

and you can rename the files as well...

also if the URI does not have file name associated in the link (might be in a database) and file.copy gives error file not found, am I able to do an impute stream and write file on external storage?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It seems like you are ignoring my answers.
they still have a file name associated with it don't they?
No.

how else are all of these after market file browsers getting names of file in different storage locations?
File browsers do not use the same API that is used ContentChooser.

and you can rename the files as well...
Irrelevant to this thread.

also if the URI does not have file name associated in the link (might be in a database) and file.copy gives error file not found,
This will never happen.

A thread is not an endless discussion. The answer to your question about File.Size was answered many times. If you have any other question, not one that was already answered here then start a new thread.
 
Upvote 0
Status
Not open for further replies.
Top