Android intent filter: associate app with file extension

后端 未结 16 1957
北海茫月
北海茫月 2020-11-22 11:06

I have a custom file type/extension that I want to associate my app with.

As far as I know, the data element is made for this purpose, but I can\'t get it working. h

16条回答
  •  耶瑟儿~
    2020-11-22 11:49

    You try this it will help for you.Instead of pdf you can use other extensions also. First you have to add read external storage permission in androidmanifest.xml file.

    
    

    Then in the androidmanifest file in the Activity tag, you add an intent-filter as shown below.

                
    
                
    
                 
    
                
    
                
    
            
    

    Finally in your code, you get path of the pdf file as shown below:

    Intent intent=getIntent();
    
    if(intent!=null) {          
    
            String action=intent.getAction();
    
            String type=intent.getType();
    
            if(Intent.ACTION_VIEW.equals(action) && type.endsWith("pdf")) {
    
                // Get the file from the intent object
    
                Uri file_uri=intent.getData();
    
                if(file_uri!=null)
    
                    filepath=file_uri.getPath();
    
                else
    
                    filepath="No file";
    
            }
    
            else if(Intent.ACTION_SEND.equals(action) && type.endsWith("pdf")){
    
                Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
    
                filepath = uri.getPath();
    
            }
    

提交回复
热议问题