Reputation: 651
I am trying to develop a folder browser for Android.. A folder can have any type of files, i.e a doc file, txt file, mp3, avi, apk file and so on.. How do i launch a specific application based on the file that the user has clicked... i mean how do i construct a file type based intent... Thanks Prashanth
Upvotes: 1
Views: 3264
Reputation: 1
may be like this...because I'm trying it on my code...
//differentiate file type
String filename = o.getName();
String filenameArray[] = filename.split("\\.");
String extension = filenameArray[filenameArray.length-1];
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
if (extension.contains("png") || extension.contains("gif") || extension.contains("jpg")
|| extension.contains("jpeg") || extension.contains("bmp"))
{
intent.setDataAndType(Uri.parse("file://" + o.getPath()), "image/*");
}
if (extension.contains("txt") || extension.contains("csv") || extension.contains("xml"))
{
intent.setDataAndType(Uri.parse("file://" + o.getPath()), "text/*");
}
if (extension.contains("pdf"))
{
intent.setDataAndType(Uri.parse("file://" + o.getPath()), "application/pdf");
}
if (extension.contains("doc") || extension.contains("docx"))
{
intent.setDataAndType(Uri.parse("file://" + o.getPath()), "application/msword");
}
if (extension.contains("mp3") || extension.contains("wav") || extension.contains("oog")
|| extension.contains("mid") || extension.contains("amr") || extension.contains("midi"))
{
intent.setDataAndType(Uri.parse("file://" + o.getPath()), "audio/*");
}
if (extension.contains("mpeg") || extension.contains("3gp"))
{
intent.setDataAndType(Uri.parse("file://" + o.getPath()), "video/*");
}
i'm still looking for the short code, but i hope this code can help you...
Upvotes: 0
Reputation: 1
I found one. Maybe this could help you:
Intent intent= new Intent();
intent.setAction(Intent.ACTION_VIEW);
File file = new File(filePath);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = file.getName().substring(file.getName().indexOf(".")+1);
String type = mime.getMimeTypeFromExtension(ext);
intent.setDataAndType(Uri.fromFile(file), type);
Upvotes: 0
Reputation: 1788
Just set the type of the Intent, for example: intent.setType("video/mpeg");
.
Edit: Use MimeTypeMap to dynamically get the MIME-Type for a file.
Upvotes: 2
Reputation: 53647
Create one activity. Where you will find the extension of file whether mp3 or dic etc. Based on the extension call another activity from this activity.
Thanks Deepak
Upvotes: 0