开发者

Android intent based on file type

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 i开发者_StackOverflowntent... Thanks Prashanth


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.


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


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);


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜