Android Intent - How can i open file

Started by
0 comments, last by frob 11 years, 10 months ago
Hello!

To open image or sound we can use this code:

public String getExt(String filename){

int dotIndex = 0;
for(int i = filename.length()-1; i >= 0; i--)
{
if(filename.charAt(i) == '.') {
dotIndex = i;
break;
}
}

return filename.substring(dotIndex + 1, filename.length());
}

public String getTypeAction(String s) {

int slashIndex = 0;
for(int i = 0; i < s.length(); i++)
{
if(s.charAt(i) == '/') {
slashIndex = i;
break;
}
}

return s.substring(0, slashIndex);
}
//////
//////

MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = getExt(file_to_action.getName());
ext = ext.toLowerCase();

String type = mime.getMimeTypeFromExtension(ext);

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file_to_action), getTypeAction(type) + "/*" );
startActivity(intent);


But the problem is when i want to open any different file for example: .mhtml, .bak. apk. When i do this my application is crash. I don't know where is the problem, i have application for .mhtml, .odt extenio but my application can't use this applications to open the file.
How i can open all file format or how i can check avaliable application for any file format ?
Advertisement
Have you carefully read the manual on how to do this? You need to make sure your manifest file is correct. If your file is not correct your app will crash, as described.


Also, If you know about the apps why not specify them by name?

This topic is closed to new replies.

Advertisement