Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert file: Uri to File in Android

What's the easiest way to convert from a file: android.net.Uri to a File in Android?

Tried the following but it doesn't work:

 final File file = new File(Environment.getExternalStorageDirectory(), "read.me");  Uri uri = Uri.fromFile(file);  File auxFile = new File(uri.toString());  assertEquals(file.getAbsolutePath(), auxFile.getAbsolutePath()); 
like image 402
hpique Avatar asked Jun 04 '10 14:06

hpique


People also ask

What is a URI Android?

A URI is a uniform resource identifier while a URL is a uniform resource locator.

How do you find the absolute path of URI?

Uri uri = data. getData(); File file = new File(uri. getPath());//create path from uri final String[] split = file. getPath().

How do I find the file path in Kotlin?

From what I know you can find the path of a file in the internal storage with java simply by using: String path = context. getFilesDir(). getAbsolutePath(); File file = new File(path + "/filename");

Is it possible to convert Uri to a file?

This don't work for image returned by: Intent photoPickerIntent = new Intent (Intent.ACTION_PICK); That case there is a need to convert Uri to real path by getRealPathFromURI () function. So the conclusion is that it depends on what type of Uri you want to convert to File. Show activity on this post.

How to get the path of a file in Android using Uri?

Uri.fromFile (file).toString () only returns something like file///storage/emulated/0/* which is a simple absolute path of a file on the sdcard but with file// prefix (scheme) You can also get content uri using MediaStore database of Android TEST (what returns Uri.fromFile and what returns MediaScannerConnection ):

How do I access other app created files in Android?

If your Android app wants to access other app created files, you should get the file from its ContentProvider with a content URI. The ContentProvider URI is not a real android file path URI ( file:///storage/41B7-12F1/DCIM/Camera/IMG_20180211_095139.jpg ), it is a content provider style URI ( content://media/external/images/media/1302716 ).

How to read/write bytes from/to file through stream in Android?

For this case, especially on Android, the way going for bytes is usually faster. With this, I solved it by setting up a class FileHelper which is given the responsibility to deal with reading/writing bytes from/to file through stream and a class UriHelper which is given the responsibility to figure out path of Uri and permission.


2 Answers

What you want is...

new File(uri.getPath()); 

... and not...

new File(uri.toString()); 

Note: uri.toString() returns a String in the format: "file:///mnt/sdcard/myPicture.jpg", whereas uri.getPath() returns a String in the format: "/mnt/sdcard/myPicture.jpg".

like image 128
Adil Hussain Avatar answered Sep 18 '22 19:09

Adil Hussain


use

InputStream inputStream = getContentResolver().openInputStream(uri);     

directly and copy the file. Also see:

https://developer.android.com/guide/topics/providers/document-provider.html

like image 39
Juan Camilo Rodriguez Durán Avatar answered Sep 17 '22 19:09

Juan Camilo Rodriguez Durán