×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: joseifm
Added: Sep 23, 2014 4:13 PM
Modified: Sep 23, 2014 4:15 PM
Views: 19
Open file, If there is no application to open the file, ask to download the google play
  1. private void openFile(String filename) {
  2.        
  3.         String path=filename;
  4.         Intent intent = new Intent();
  5.         intent.setAction(android.content.Intent.ACTION_VIEW);
  6.         File file = new File(path);
  7.  
  8.         MimeTypeMap mime = MimeTypeMap.getSingleton();
  9.         final String ext=file.getName().substring(file.getName().indexOf(".") + 1).toLowerCase();
  10.         final String type2 = mime.getMimeTypeFromExtension(ext);
  11.         intent.setDataAndType(Uri.fromFile(file),type2);
  12.  
  13.         try{
  14.             startActivity(intent);
  15.         }catch ( ActivityNotFoundException e){
  16.  
  17.             // If there is no application to open the file, ask to download the google play
  18.  
  19.             AlertDialog.Builder builder = new AlertDialog.Builder(this);
  20.             builder.setTitle(R.string.application_error);
  21.             builder.setMessage(R.string.application_download);
  22.             builder.setPositiveButton(R.string.button_yes,
  23.                     new DialogInterface.OnClickListener() {
  24.                         @Override
  25.                         public void onClick(DialogInterface dialog, int which) {
  26.                             Intent marketIntent = new Intent(Intent.ACTION_VIEW);
  27.                             marketIntent
  28.                                     .setData(Uri
  29.                                             .parse("market://search?q=<seach_query>&c=" + ext ));
  30.  
  31.                             startActivity(marketIntent);
  32.                         }
  33.                     });
  34.             builder.setNegativeButton(R.string.button_no, null);
  35.             builder.create().show();
  36.         }
  37.     }