Action_View Intent for a File with Unknown Mimetype

ACTION_VIEW intent for a file with unknown MimeType

This should detect the mimetype and open with default:

MimeTypeMap myMime = MimeTypeMap.getSingleton();
Intent newIntent = new Intent(Intent.ACTION_VIEW);
String mimeType = myMime.getMimeTypeFromExtension(fileExt(getFile()).substring(1));
newIntent.setDataAndType(Uri.fromFile(getFile()),mimeType);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(newIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(context, "No handler for this type of file.", Toast.LENGTH_LONG).show();
}

Using this function:

private String fileExt(String url) {
if (url.indexOf("?") > -1) {
url = url.substring(0, url.indexOf("?"));
}
if (url.lastIndexOf(".") == -1) {
return null;
} else {
String ext = url.substring(url.lastIndexOf(".") + 1);
if (ext.indexOf("%") > -1) {
ext = ext.substring(0, ext.indexOf("%"));
}
if (ext.indexOf("/") > -1) {
ext = ext.substring(0, ext.indexOf("/"));
}
return ext.toLowerCase();

}
}

Android - How do I open a file in another app via Intent?

Posting my changes here in case it can help someone else. I ended up changing the download location to an internal folder and adding a content provider.

public void open_file(String filename) {
File path = new File(getFilesDir(), "dl");
File file = new File(path, filename);

// Get URI and MIME type of file
Uri uri = FileProvider.getUriForFile(this, App.PACKAGE_NAME + ".fileprovider", file);
String mime = getContentResolver().getType(uri);

// Open file with user selected app
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, mime);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}

Android (Intent.ACTION_VIEW) not recognizing on Mi device for mimeType video/mp4

/storage/emulated/0/DCIM/Camera/VID_20160113_130138.mp4 is not a valid string representation of a Uri. A Uri needs a scheme.

Presumably, once upon a time, you had a File object for this. Use that, and Uri.fromFile(), instead of Uri.parse(). Or, use Uri.fromFile(new File(path)). This will give you the proper scheme setup.

Launching an intent for file and MIME type?

Well thanks to the Open Intent guys, I missed the answer the first time through their code in their file manager, here's what I ended up with:

    File file = new File(filePath);
MimeTypeMap map = MimeTypeMap.getSingleton();
String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName());
String type = map.getMimeTypeFromExtension(ext);

if (type == null)
type = "*/*";

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.fromFile(file);

intent.setDataAndType(data, type);

startActivity(intent);

If you use a mime type of "* / *" when you can't determine it from the system(it is null) it fires the appropriate select application dialog.

Is it possible for ACTION_VIEW with a MIME type of pdf to find no application which could handle it?

Never trust that the user will have an application which will be able to open the file you wanted to. You never know what applications user has installed on his/her device and etc.
Always check :

try {
startActivity(chooser)
} catch (e: ActivityNotFoundException) {
// Define what your app should do if no activity can handle the intent.
}

Better to be prepared instead of crashing the app.

Open File With Intent (Android)

Call addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION). Otherwise, the other app will not have rights to work with the content identified by that Uri.

Note that this only works if you have rights to work with the content identified by that Uri.

how to open files using intent action with latest Android storage framework?

After the hints from the comments, I found the answer in developer docs.

Caution: If you want to set both the URI and MIME type, don't call setData() and setType() because they each nullify the value of the other. Always use setDataAndType() to set both URI and MIME type.

The reason behind openFile didn't throw FileUriExposedException in android-storage-samples is that after setting intent.type, the Uri gets nullified and when I changed it to setDataAndType() I got the exception. The final snippet looks like

fun openFile(activity: AppCompatActivity, selectedItem: File) {
// Get URI and MIME type of file
val uri = FileProvider.getUriForFile(activity.applicationContext, AUTHORITY, selectedItem)
// val uri = Uri.fromFile(selectedItem).normalizeScheme()
val mime: String = getMimeType(uri.toString())

// Open file with user selected app
val intent = Intent()
intent.action = Intent.ACTION_VIEW
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
// intent.data = uri
// intent.type = mime
intent.setDataAndType(uri, mime)
return activity.startActivity(intent)
}

I think they forgot to update the samples over time, let me create a pull request to commit this change over there as well.



Related Topics



Leave a reply



Submit