Tuesday 18 October 2011

android : Open folder with default application using Intents without Mime Types

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

7 comments:

  1. Great - Thanks.

    Only "String ext line" should be better with:
    .....lastIndexOf(".") + 1).toLowerCase();

    ReplyDelete
    Replies
    1. If we want to be really pedantic, :o) it should be:

      .....lastIndexOf(".") + 1).toLowerCase(Locale.US);

      Delete
  2. String path = "File Path";
    Is this has to be specific file location?

    what if i want the user to select file to open, how i would do that?

    ReplyDelete
  3. Thank you very much. Second one worked with me

    ReplyDelete
  4. i am new to the android studio and i dont know where to put this method in a code,can anyone help me with this

    ReplyDelete
  5. Jammy O'Clock - Slot machine - JAMMY O'Clock - jtmhub.com
    Get jammy O'Clock slot 구미 출장마사지 machine at JAMMY 수원 출장샵 O'Clock. Take the 울산광역 출장안마 slot 제천 출장마사지 machine game for a spin and win a fortune! 포항 출장마사지

    ReplyDelete