ContentResolver always returns null

I am trying to get the content type of a local file on my Android device. My code

File file = new File(uploadPath.replace("file://","")); Uri uri = Uri.fromFile(file); ContentResolver contentResolver = getApplicationContext().getContentResolver(); String type = contentResolver.getType(uri); 

My download path is file:///storage/emulated/0/DCIM/Camera/20141016_181148.jpg . However, I always get my type as null. Why is this??

+8
android
source share
3 answers

Content recombinant will return null if uri scheme is not content: //

+2
source share

to get the file type, look there: How to determine the MIME file type in android?

Reasons for a possible null value:

What causes ContentResolver.query () for Android to return null?

from what i see, uri is probably wrong.

0
source share

If you work with the latest coding standards, there are many changes regarding storage management and access. To answer your question, you should replace Uri uri = Uri.fromFile(file); to the following code:

Java:

  Uri uri = FileProvider.getUriForFile( context, context.getPackageName() + ".provider", file ); 

Kotlin:

 val uri = FileProvider.getUriForFile( context, context.packageName + ".provider", file ) 

Remember to check the official documentation for

Hope this helps!

0
source share

All Articles