I'm trying to get the content type of a local file on my android device. My code is
File file = new File(uploadPath.replace("file://",""));
Uri uri = Uri.fromFile(file);
ContentResolver contentResolver = getApplicationContext().getContentResolver();
String type = contentResolver.getType(uri);
My upload path is file:///storage/emulated/0/DCIM/Camera/20141016_181148.jpg
. However, I always get my type
as null. Why is that??
The ContentResolver. query() client method always returns a Cursor containing the columns specified by the query's projection for the rows that match the query's selection criteria. A Cursor object provides random read access to the rows and columns it contains.
The Content Resolver behaves exactly as its name implies: it accepts requests from clients, and resolves these requests by directing them to the content provider with a distinct authority. To do this, the Content Resolver stores a mapping from authorities to Content Providers.
If you're working with latest coding standards, there are a lot of changes in terms of how the storage is managed and we access it. To answer your question, you got to replace Uri uri = Uri.fromFile(file);
with below code:
Java:
Uri uri = FileProvider.getUriForFile(
context,
context.getPackageName() + ".provider",
file
);
Kotlin:
val uri = FileProvider.getUriForFile(
context,
context.packageName + ".provider",
file
)
Don't forget to check the official documentation on
I hope this helps!
The content resolver will return null if the uri scheme is not content://
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With