Sunday, April 15, 2012

open pdf,audio etc type of file by intent


public void openFile(String pathWithFileName,String type)
    {
        String fileType=determineFileType(type);
     
        File file = new File(pathWithFileName);

        if (file.exists())
        {
            Uri path = Uri.fromFile(file);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(path,fileType);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            try
            {
                startActivity(intent);
            }
            catch (ActivityNotFoundException e)
            {
                Toast.makeText(Main.this,
                    "No Application Available to View PDF",
                    Toast.LENGTH_SHORT).show();
            }
        }
    } //openFile()
 
    public String determineFileType(String type)
    {
        String fileType="";
        if(type.equals("audio"))
        {
            fileType="audio/*";
        }
        else if(type.equals("pdf"))
        {
            fileType="application/pdf";
        }
     
        return fileType;
    } //determinrFileType()

No comments:

Post a Comment