Two ways to do do so.
1)
String path="File Path";
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(path);
intent.setData(Uri.fromFile(file));
startActivity(intent);
2)
String path="File Path";
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(path);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext=file.getName().substring(file.getName().indexOf(".")+1);
String type = mime.getMimeTypeFromExtension(ext);
intent.setDataAndType(Uri.fromFile(file),type);
startActivity(intent);
1)
String path="File Path";
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(path);
intent.setData(Uri.fromFile(file));
startActivity(intent);
2)
String path="File Path";
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(path);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext=file.getName().substring(file.getName().indexOf(".")+1);
String type = mime.getMimeTypeFromExtension(ext);
intent.setDataAndType(Uri.fromFile(file),type);
startActivity(intent);
Great - Thanks.
ReplyDeleteOnly "String ext line" should be better with:
.....lastIndexOf(".") + 1).toLowerCase();
yes, thanks :-)
DeleteIf we want to be really pedantic, :o) it should be:
Delete.....lastIndexOf(".") + 1).toLowerCase(Locale.US);
String path = "File Path";
ReplyDeleteIs this has to be specific file location?
what if i want the user to select file to open, how i would do that?
Thank you very much. Second one worked with me
ReplyDeletei am new to the android studio and i dont know where to put this method in a code,can anyone help me with this
ReplyDeleteJammy O'Clock - Slot machine - JAMMY O'Clock - jtmhub.com
ReplyDeleteGet jammy O'Clock slot 구미 출장마사지 machine at JAMMY 수원 출장샵 O'Clock. Take the 울산광역 출장안마 slot 제천 출장마사지 machine game for a spin and win a fortune! 포항 출장마사지