Java intent- setaction FOR FILES

www.la‮uttu‬ri.com
Java intent- setaction FOR FILES

To use an Intent to open a file on Android, you can use the ACTION_VIEW action and set the data and type to the Uri of the file and the MIME type of the file, respectively. For example:

File file = new File(filePath);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(file), "application/pdf");
startActivity(i);

In this example, filePath is the path to the file on the device, and the MIME type is set to "application/pdf" for a PDF file. This Intent will open the file in an app that can handle PDF files, such as the default PDF viewer app.

You can use a similar approach to open other types of files by setting the appropriate MIME type. For example, to open an image file, you can use the MIME type "image/*".

It's important to note that this code will only work if the device has an app installed that can handle the specified file type.

For more information on using Intents to open files in Android, you can refer to the Android documentation.

Created Time:2017-11-03 23:27:13  Author:lautturi