Skip to content Skip to sidebar Skip to footer

Check User Action On Intent.action_view

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 u

Solution 1:

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

AlertDialogIntentChooseralertDialog=newAlertDialogIntentChooser(filePath,getActivity());
alertDialog.show();

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

publicclassAlertDialogIntentChooser {
private String filePath;
private Activity activity;
private AlertDialog dialog;
private AlertDialogDelegate delegate;
private ListItem[] items;

publicAlertDialogIntentChooser(String filePath,Activity activity){
    this.filePath = filePath;
    this.activity = activity;
    init();
}

publicvoidsetDialogDelegate(AlertDialogDelegate delegate){
    this.delegate = delegate;
}

privatevoidinit(){

    initApplicationItems();

    AlertDialog.Builderbuilder=newAlertDialog.Builder(activity);
    builder.setTitle(Strings.STRING_SELECT_APPLICATION);
    builder.setIcon(R.drawable.ic_share);

    builder.setOnCancelListener(newOnCancelListener() {

        @OverridepublicvoidonCancel(DialogInterface paramDialogInterface) {
            if(delegate!=null)
                delegate.onDialogCancelled(paramDialogInterface);
        }
    });

    builder.setAdapter(adapter, newDialogInterface.OnClickListener() {
        @OverridepublicvoidonClick(DialogInterface dialog, int which) {         

            IntentintentPdf=newIntent(Intent.ACTION_VIEW);
            MimeTypeMapmyMime= MimeTypeMap.getSingleton();
            StringfileExt= MimeTypeMap.getFileExtensionFromUrl(Uri.parse(filePath));
            StringmimeType= 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();        
}

privatevoidinitApplicationItems(){
    IntentintentPdf=newIntent(Intent.ACTION_VIEW);

    MimeTypeMapmyMime= MimeTypeMap.getSingleton();
    StringfileExt= MimeTypeMap.getFileExtensionFromUrl(Uri.parse(filePath));
    StringmimeType= myMime.getMimeTypeFromExtension(fileExt);             
    intentPdf.setDataAndType(Uri.parse(filePath), mimeType);
    PackageManagerpm= activity.getPackageManager();
    List<ResolveInfo> resInfos = pm.queryIntentActivities(intentPdf, 0);

    items = newListItem[resInfos.size()];
    inti=0;
    for (ResolveInfo resInfo : resInfos) {
        Stringcontext= resInfo.activityInfo.packageName;
        StringpackageClassName= resInfo.activityInfo.name;
        CharSequencelabel= resInfo.loadLabel(pm);
        Drawableicon= resInfo.loadIcon(pm);
        items[i] = newListItem(label.toString(), icon, context, packageClassName);
        ++i;
    }
}

publicvoidshow(){
    dialog.show();
}

privateListAdapteradapter=newArrayAdapter<ListItem>(
          activity,
    android.R.layout.select_dialog_item,
    android.R.id.text1,
    items){

    public View getView(int position, View convertView, ViewGroup parent) {

        Viewv=super.getView(position, convertView, parent);
        TextViewtv= (TextView)v.findViewById(android.R.id.text1);

        intdpS= (int) (72 * activity.getResources().getDisplayMetrics().density *  0.5f);
        items[position].icon.setBounds(0, 0, dpS, dpS);
        tv.setCompoundDrawables(items[position].icon, null, null, null);

        intdp5= (int) (5 * activity.getResources().getDisplayMetrics().density *  0.5f);
        tv.setCompoundDrawablePadding(dp5);

        return v;
    }
};

classListItem {
     publicfinal String name;
     publicfinal Drawable icon;
     publicfinal String context;
     publicfinal String packageClassName;

     publicListItem(String text, Drawable icon, String context, String packageClassName) {
         this.name = text;
         this.icon = icon;
         this.context = context;
         this.packageClassName = packageClassName;
     }

     @Overridepublic String toString() {
         return name;
     }
 }

 publicstaticinterfaceAlertDialogDelegate{
     publicvoidonDialogCancelled(DialogInterface paramDialogInterface);
     publicvoidonItemSelected(String packageName, String className);
 }
}

Solution 2:

try

if (resultCode == RESULT_CANCELED) instead of if (resultCode == Activity.RESULT_CANCELED )

Solution 3:

try this..

Intentintent=newIntent(Intent.ACTION_VIEW);     
        intent.setDataAndType(Uri.parse(filePath),mimetype);
        startActivity(intent);

Post a Comment for "Check User Action On Intent.action_view"