Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8640449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:16:59+00:00 2026-06-12T11:16:59+00:00

I have a listview populated with some files,there can be various types like pdf

  • 0

I have a listview populated with some files,there can be various types like pdf or documents.When a user clicks on one i get the file mime type and start an intent that let’s the user choose which application to use to open that file.What i want is to know is a user choosed something,or simply pressed back and didn’t choose anything.
What i tried untill now was doing a startActivityForResult and checking for success,but it returns always RESULT_CANCELED

    static final int SELECTED_VIEWER = 1;

    Intent intent = new Intent(Intent.ACTION_VIEW);     
    intent.setDataAndType(Uri.parse(filePath), mimetype);
    try {
        startActivityForResult(intent, SELECTED_VIEWER);
    }catch (ActivityNotFoundException e) {
        Toast.makeText(getActivity(), 
           Strings.ERROR_NO_VIEWER, 
            Toast.LENGTH_SHORT).show();
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
     if (requestCode == SELECTED_VIEWER) {
         if (resultCode == Activity.RESULT_CANCELED ) {
             //do something
         }
     }
 }

I even tried with an startActivityForResult(Intent.createChooser but still to no avail.
How can i know if the user choosed something on that list,or if he cancelled the open?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:17:01+00:00Added an answer on June 12, 2026 at 11:17 am

    As written in Android Developer on Activities

    In other protocols (such as ACTION_MAIN or ACTION_VIEW), you may not get the result when you expect.

    You can’t count on action views returning what you would expect,so what i did was implement a custom alert dialog that shows all possible applications that can open a certain file,a slightly modified version as shown here Custom intent chooser

    Code for those wondering,it takes a filePath as parameter and shows you all installed applications that can handle that filetype by getting the mimetype.Works with fullpaths.Can be called with

    AlertDialogIntentChooser alertDialog = new  AlertDialogIntentChooser(filePath,getActivity());
    alertDialog.show();
    

    this is the class,it can take an optional delegate aswell for activity callbacks

    public class AlertDialogIntentChooser {
    private String filePath;
    private Activity activity;
    private AlertDialog dialog;
    private AlertDialogDelegate delegate;
    private ListItem[] items;
    
    public AlertDialogIntentChooser(String filePath,Activity activity){
        this.filePath = filePath;
        this.activity = activity;
        init();
    }
    
    public void setDialogDelegate(AlertDialogDelegate delegate){
        this.delegate = delegate;
    }
    
    private void init(){
    
        initApplicationItems();
    
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(Strings.STRING_SELECT_APPLICATION);
        builder.setIcon(R.drawable.ic_share);
    
        builder.setOnCancelListener(new OnCancelListener() {
    
            @Override
            public void onCancel(DialogInterface paramDialogInterface) {
                if(delegate!=null)
                    delegate.onDialogCancelled(paramDialogInterface);
            }
        });
    
        builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {         
    
                Intent intentPdf = new Intent(Intent.ACTION_VIEW);
                MimeTypeMap myMime = MimeTypeMap.getSingleton();
                String fileExt = MimeTypeMap.getFileExtensionFromUrl(Uri.parse(filePath));
                String mimeType = myMime.getMimeTypeFromExtension(fileExt);                 
                intentPdf.setClassName(items[which].context, items[which].packageClassName);
                intentPdf.setDataAndType(Uri.parse(filePath), mimeType);
                try {
                    activity.startActivity(intentPdf);
                    dialog.dismiss();
                    if(delegate!=null)
                        delegate.onItemSelected(items[which].context, items[which].packageClassName);
                }catch (ActivityNotFoundException e) {
                    Toast.makeText(activity, 
                            Strings.ERROR_NO_VIEWER, 
                            Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                }               
            }
        });
    
        dialog = builder.create();        
    }
    
    private void initApplicationItems(){
        Intent intentPdf = new Intent(Intent.ACTION_VIEW);
    
        MimeTypeMap myMime = MimeTypeMap.getSingleton();
        String fileExt = MimeTypeMap.getFileExtensionFromUrl(Uri.parse(filePath));
        String mimeType = myMime.getMimeTypeFromExtension(fileExt);             
        intentPdf.setDataAndType(Uri.parse(filePath), mimeType);
        PackageManager pm = activity.getPackageManager();
        List<ResolveInfo> resInfos = pm.queryIntentActivities(intentPdf, 0);
    
        items = new ListItem[resInfos.size()];
        int i = 0;
        for (ResolveInfo resInfo : resInfos) {
            String context = resInfo.activityInfo.packageName;
            String packageClassName = resInfo.activityInfo.name;
            CharSequence label = resInfo.loadLabel(pm);
            Drawable icon = resInfo.loadIcon(pm);
            items[i] = new ListItem(label.toString(), icon, context, packageClassName);
            ++i;
        }
    }
    
    public void show(){
        dialog.show();
    }
    
    private ListAdapter adapter = new ArrayAdapter<ListItem>(
              activity,
        android.R.layout.select_dialog_item,
        android.R.id.text1,
        items){
    
        public View getView(int position, View convertView, ViewGroup parent) {
    
            View v = super.getView(position, convertView, parent);
            TextView tv = (TextView)v.findViewById(android.R.id.text1);
    
            int dpS = (int) (72 * activity.getResources().getDisplayMetrics().density *  0.5f);
            items[position].icon.setBounds(0, 0, dpS, dpS);
            tv.setCompoundDrawables(items[position].icon, null, null, null);
    
            int dp5 = (int) (5 * activity.getResources().getDisplayMetrics().density *  0.5f);
            tv.setCompoundDrawablePadding(dp5);
    
            return v;
        }
    };
    
    class ListItem {
         public final String name;
         public final Drawable icon;
         public final String context;
         public final String packageClassName;
    
         public ListItem(String text, Drawable icon, String context, String packageClassName) {
             this.name = text;
             this.icon = icon;
             this.context = context;
             this.packageClassName = packageClassName;
         }
    
         @Override
         public String toString() {
             return name;
         }
     }
    
     public static interface AlertDialogDelegate{
         public void onDialogCancelled(DialogInterface paramDialogInterface);
         public void onItemSelected(String packageName, String className);
     }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 3 ListView's that are populated like this (well this can be anything,
I have listview control.There is an option to remove selected items.After the user removes
I have listview having customized some textview and one imageview. When I long click
I have a listview, basically I would like to have the Listview populated and
I have a ListView that is being populated with a custom adapter. I'd like
I have a listView vith some items. I would like to get from my
I have a ListView which is populated using a CursorAdapter. I'd also like to
I have a ListView that is populated using an XML file. However, I want
I am currently implementing a ListView populated with CheckedTextViews and have everything working just
I have a ListView in a ListActivity populated with strings. However, only the text-part

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.