I have a File object and I want to get its Uri, that looks like:
content://media/external/images/media/2683
I've tried the following code:
val uri = FileProvider.getUriForFile(activity, "${BuildConfig.APPLICATION_ID}.file_provider", pictureFile)
But I'm getting this:
content://my.package.name.file_provider/external_files/Pictures/photo-1574403815163.jpg
Is it possible to get the Uri I need?
UPD: The reason I want to get another Uri - I can't create File object from this Uri later.
val uri = FileProvider.getUriForFile(activity, "${BuildConfig.APPLICATION_ID}.file_provider", pictureFile)
val file = File(uri.path)
When I try to file.exists() I'm always getting false.
I can't create File object with this Uri
That's because you don't do that. Moreover, you don't need that. pictureFile is a File.
If your are creating file with actual path then you just concad the content:// to that path
Iam not sure this is correct method or not but i get that what you expected , Try this one
Related
How do we check if an Android Uri(android.net.Uri) object points to a file or a directory, assuming that the Uri exists?
Thanks.
You can parse the URI and identify is it file or not.
val pointToFile = uri.scheme == "file"
Try to open an InputStream for the uri.
If you succeed it's a file.
Don't forget to close the stream.
Further you can use a content resolver to query for mime type. There exists mime type dir.
We need to convert a file path to a content uri and pass on for subsequent processing to a common component. When we use "Uri.fromFile(file)" it returns a file uri and not a content uri. Please let us know how to get this converted. Appreciate your help.
for (File file : fileBaseFolder.listFiles()) {
Uri convertedUri = Uri.fromFile(file);//Need to convert this Uri to a Content uri
.........
}
Use FileProvider to serve your file.
It gives nice uries.
FileProvider.getUriForFile().
You can serve all files except those from removable micro sd card.
try MediaScannerConnection.scanFile
for (File file : fileBaseFolder.listFiles()) {
MediaScannerConnection.scanFile(this, new String[] { file.getAbsolutePath() }, null,
(path, uri) -> Log.i(TAG, uri.toString()));
}
Take a look at the below link for the solution.
https://www.thetopsites.net/article/52460984.shtml
Also, the same question is asked here.
I am getting uri using GET_CONTENT and also it works when used in Glide to show the image. But I am not getting file size. It always shows 0.
I tried using File(uri.path).length()
fun fileSize(uri: Uri){
var a = File(uri.path).length()
}
A Uri is not a file.
Add a dependency for DocumentFile, such as androidx.documentfile:documentfile. Then, replace your code with:
fun fileSize(uri: Uri){
var a = DocumentFile.fromSingleUri(uri).length()
}
The File constructor takes a file system pathname as as its argument. You have given it a "path" extracted from a URI which is probably not a pathname for a file system object. (It might be if you are using a file: URL, but you are not checking the schema, so you can't be sure.)
If your File object has a pathname for a non-existent file, then when you call File::length, the documentation states that length() will return zero.
In the Java world you could try the following:
URI uri = ...
File file = new File(uri);
long length = file.length();
However, this only works if:
The URI is an absolute URI with a file: schema.
The file that the URI denotes exists in the local file system namespace.
(If 1. is not true, you get an exception. If 2. is not true, you get a zero result.)
This should also work on the Android platform ... without adding any extra dependencies.
I am using directory selection as described in this Google Sample. It does provide file name and mime type of the children of the selected directory. I can get Document ID of the file too, if I use COLUMN_DOCUMENT_ID on the Cursor Query.
I am interested in the file URI of the children instead. When I use ACTION_OPEN_DOCUMENT instead of ACTION_OPEN_DOCUMENT_TREE, I get the child uri easily which is just obtained from adding a %2Fchildfile.extention (%2F is just a forward slash). So I tried to get child file uri using the following code -
uri = Uri.parse(docUri.toString()+"%2F"+fileName);
I got the file name, however when I run exists() method on it (By converting it into DocumentFile), it returns false. That means, either I don't have the permission of the file or it's not the correct way to get children uri.
Am I missing something here or is there any other way I can select a folder and get file uri of all of it's children easily.
PS: I am currently checking it in Marshamallow.
After reading the doc and trying out certain examples, I got the following way to get a single file Uri from a selected docUri/treeUri
uri = DocumentsContract.buildDocumentUriUsingTree(docUri,docId);
And then you can convert it anytime into a DocumentFile using following code -
DocumentFile file = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (DocumentsContract.isDocumentUri(context, uri)) {
file = DocumentFile.fromSingleUri(context, uri);
} else {
file = DocumentFile.fromTreeUri(context, uri);
}
}
fromTreeUri() method is required for the selected Tree Directory, so that it can return true on file.exists() method call.
You need to remember that if the children contain any directory, then you can't call childDirectory.listFiles() on it. It'll give UnsupportedOperationException, because you don't have permission to access the child directory's file. Read more about this here.
I'm trying to retrieve the file that the user wants to share via, for example, the Photo Gallery App. So, when they tap on the share button, and select my application, I'm retrieving the File's Uri through the method Uri fileUri = (Uri) getIntent().getExtras().get(Intent.EXTRA_STREAM);. This works fine.
Then, I try to get the File whose Uri is the one I have. I do that this way:
File file = new File(fileUri.getPath());
But, afterwards, when I try to open it and read it, I get the java.io.FileNotFoundException ENOENT (No such file or directory). Furthermore, when I execute the file.exists() method, it returns false. So I don't know where the problem is.
Here is the code:
Uri fileUri = (Uri) getIntent().getExtras().get(Intent.EXTRA_STREAM);
File file = new File(fileUri.getPath());
FileInputStream imageInFile = new FileInputStream(file);
What am I doing wrong?
EDIT:
I already have the <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> tag in my manifest.
I must also add that the uri of the file which I retrieve is something like: "/0/1/content:/media/external/images/media/9412/ACTUAL/1989334571". As you can see, the file doesn't have any extension at all, so that might be the problem. Nevertheless, I don't know how to solve it.
EDIT 2:
It seems as it is a problem that started since KitKat...