Thursday, February 2, 2012

code to display .pdf,.text,.DOC,.DOCX,.doc extension files exists in SDCard

///here in below code use "file(.txt,.pdf,.doc ) path that exists in SDCard in place of
"imageofFileArayList.get(c1)".means replace "imageofFileArayList.get(c1)" this with file path like"/mnt/sdcard/Sample.pdf"

if(imageofFileArayList.get(c1)!=null){
    
     File file = new File(imageofFileArayList.get(c1));
     String filename=file.getName();
     Intent intent= new Intent(getActivity(),ShowCaseFileImageActivity.class);
         
           Uri filetype= Uri.parse(file.getPath());
     if(filename.endsWith(".gif")||filename.endsWith(".jpg")||filename.endsWith(".png")||filename.endsWith(".jpeg") ||filename.endsWith(".JPEG"))
           {
      Log.d("HI1","image view clicked"+c1);     
      ShowCaseFileImageFrag newFragment = new ShowCaseFileImageFrag();     
      Bundle b = new Bundle();           
      b.putString("imageuri",imageofFileArayList.get(c1)); 
     newFragment.setArguments(b);          
     Log.d("Details traffic cases frag","is tablet flag"+SharedVariables.isTablet);
     Log.d("Details traffic cases frag","image path"+imageofFileArayList.get(c1));
    
           if(SharedVariables.isTablet){/////if u use tablet phone,this code will execute
            MainMenuScreen objMain=(MainMenuScreen)getActivity();
            objMain.showDetailsFragment(newFragment);  
    }else{
       intent= new Intent(getActivity(),ShowCaseFileImageActivity.class);
      intent.putExtras(b);
      startActivity(intent);
      }
           }///if(.jpg,.gif)
     else
     {
      if(filename.endsWith(".txt") || filename.endsWith(".doc")||filename.endsWith(".DOC") || filename.endsWith(".docx")||filename.endsWith(".DOCX"))
            {
       if(filename.endsWith(".txt")){
        System.out.println("---txt/doc file exists");
        Uri path = Uri.fromFile(file);
        Intent intent1= new Intent(Intent.ACTION_VIEW);             
              intent1.setDataAndType(path, "text/html");//---->application/msword
              startActivity(intent1);
       /* StringBuilder builder=new StringBuilder();
        try{                            
                  FileInputStream fileIS = new FileInputStream(file);                 
                  BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
                  String readString = new String();
                  //just reading each line and pass it on the debugger
                  while((readString = buf.readLine())!= null){           
                   builder.append(readString);
                     Log.d("line: ", readString);
                  }  
                        
               } catch (FileNotFoundException e) {
                  e.printStackTrace();
               } catch (IOException e){
                  e.printStackTrace();
               }        
       
        Bundle bundle = new Bundle();           
        bundle.putString("evidencetext",builder.toString());       
        Intent notesIntent = new Intent(getActivity(), ShowEvidenceText.class).putExtras(bundle);
        startActivity(notesIntent);
       
       }*/
       else{
       /*System.out.println("---txt/doc file exists");
       Uri path = Uri.fromFile(file);
       Intent intent1= new Intent(Intent.ACTION_VIEW);             
             intent1.setDataAndType(path, "application/*");//---->application/msword
             startActivity(intent1);*/
            
      
           /*//sample code to Display all installed applications in Device
       boolean thickSoftFlag=false;
             final PackageManager pm = getActivity().getPackageManager();
             String str="com.tf.thinkdroid.amlite";
           //get a list of installed apps.
                   List<ApplicationInfo> packages = pm
                           .getInstalledApplications(PackageManager.GET_META_DATA);
                   for (ApplicationInfo packageInfo : packages) {
                       Log.d("TAG", "Installed package :" + packageInfo.packageName);
                       if((packageInfo.packageName).equalsIgnoreCase(str)){
                        System.out.println("*****s/w installed");
                        thickSoftFlag=true;}
                       Log.d("TAG",
                               "Launch Activity :"
                                       + pm.getLaunchIntentForPackage(packageInfo.packageName));
                   }
             if(thickSoftFlag){*/
              System.out.println("---Its a doc file");
        Uri path = Uri.fromFile(file);
        Intent intent1= new Intent(Intent.ACTION_VIEW);             
              intent1.setDataAndType(path, "application/*");//---->application/msword
              startActivity(intent1);
            
            /* }
             else
              showAlert("Please Install ThinkFree Office Mobile Viewer Software in Your Device");  */          
            
            
       /*System.out.println("---txt/doc file exists");
       String extension =
             MimeTypeMap.getFileExtensionFromUrl(filename);
             Log.i("extension QQQ ",extension);
             String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
             System.out.println("MIME TYPE "+mimeType);
             File fileToRead = new File(filename);
             Intent i2 = new Intent(Intent.ACTION_VIEW);
             String type="text/"+extension;
             System.out.println("TYPE "+type);
             //i2.setAction(android.content.Intent.ACTION_VIEW);
             i2.setDataAndType(Uri.fromFile(file), "application/msword");
             startActivity(i2);*/      
            
            }}
     
     
      else
      {
       if(filename.endsWith(".pdf"))
             { System.out.println("---pdf file exists");          
       
           Uri path = Uri.fromFile(file);
                  Intent intent1 = new Intent(Intent.ACTION_VIEW);
                  intent1.setDataAndType(path, "application/pdf");
                  intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  try {
                      startActivity(intent1);
                  }
                  catch (ActivityNotFoundException e) {
                      Toast.makeText(getActivity(),
                          "No Application Available to View PDF",
                          Toast.LENGTH_SHORT).show();
                  }
             }     
      }     
     }
     }//if
     else{
      showAlert("There is no content to Display");
     }



Here above code i used in "JOR Tabbed Phone" project in the activity "Details_TrafficCasesFrag"  

3 comments:

  1. can you share for this project. I'm a newbie and trying to open this type of document.
    thanks

    ReplyDelete
    Replies
    1. Hey wat do u mean,do u need extra stuff from me

      Delete
    2. oh, I want to know more about how to read document. In this example, you use intent to open. Do you get plaint text and metadata when you use this method? I want to convert them to show on webview. So, can you show me the way to do this?
      thanks

      Delete