Android file extension from a URI

To get a file’s extension from a URI in Android, you can use the ContentResolver and DocumentFile classes. Here’s an example of how you can achieve this:

import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.webkit.MimeTypeMap;

public class FileUtils {

    public static String getFileExtension(Context context, Uri uri) {
        ContentResolver contentResolver = context.getContentResolver();

        MimeTypeMap mime = MimeTypeMap.getSingleton();
        String type = contentResolver.getType(uri);
        String extension;

        if (type != null) {
            extension = mime.getExtensionFromMimeType(type);
        } else {
            // If the type is null, try to extract the extension from the URI itself
            String uriString = uri.toString();
            int lastDot = uriString.lastIndexOf('.');
            if (lastDot != -1) {
                extension = uriString.substring(lastDot + 1);
            } else {
                extension = null;
            }
        }

        return extension;
    }
}

n this example, getFileExtension takes a Context and a Uri as parameters and returns the file extension. It first tries to get the file extension from the MIME type using MimeTypeMap. If the MIME type is null, it attempts to extract the extension from the URI itself by finding the last dot in the URI.

If you are using ActivityResultContracts.GetContent() to launch the file picker and receive the selected URI.

To get the file’s extension from the URI, you can use the ContentResolver and MimeTypeMap as shown in the previous example. Here’s how you can modify your code:

val launch = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
    uri?.let {
        val extension = getFileExtension(context, it)

        if (extension == "db") {
            coroutine.launch(Dispatchers.IO) {
                profileVM.load(it, context)
            }
        }
    }
}

// Call the launcher
launch.launch("application/*") // You can specify the MIME type or use "*/*" for all file types

And include the getFileExtension function in your code:

import android.content.ContentResolver
import android.content.Context
import android.net.Uri
import android.webkit.MimeTypeMap

fun getFileExtension(context: Context, uri: Uri): String? {
    val contentResolver: ContentResolver = context.contentResolver

    // Try to get the file extension from the MIME type
    val mimeTypeMap: MimeTypeMap = MimeTypeMap.getSingleton()
    val type: String? = contentResolver.getType(uri)
    val extension: String? = type?.let { mimeTypeMap.getExtensionFromMimeType(it) }

    if (extension != null) {
        return extension
    }

    // If the MIME type is null, try to extract the extension from the URI itself
    val uriString: String = uri.toString()
    val lastDot = uriString.lastIndexOf('.')
    return if (lastDot != -1) {
        uriString.substring(lastDot + 1)
    } else {
        null
    }
}

This way, you can check if the selected file has the “db” extension before proceeding with further actions.

Remember to handle permission requests and other necessary checks when working with URIs. Additionally, if there have been changes or additions to the Android API after my last update, you may want to refer to the latest Android documentation for the most up-to-date information.

To get a file’s extension from a URI in Android, you can use the ContentResolver and DocumentFile classes. Here’s an example of how you can achieve this: n this example, getFileExtension takes a Context and a Uri as parameters and returns the file extension. It first tries to get the file extension from the MIME…

Leave a Reply

Your email address will not be published. Required fields are marked *