android 打开pdf文档

谢飞舟
2023-12-01

http://hi.baidu.com/_c211/item/54f72e19593d48f287ad4e31




  File file = new File("/data/data/com.example.soundtest/","ww.pdf");  
					        
					           
					        //File file = new File( "/data/data/com.aiai.www/databases", "attribution.db");  
					        if(copyPdf(file))
					        	{
					        		file.setReadable(true,false);
							        file.setWritable(true,false);
					        	openPdf();
					        	}
							 

public Boolean copyPdf(File file){  
        try{  
        if(!file.exists()){ InputStream is = this.getResources().openRawResource(  
                R.raw.aa); //欲导入的数据库  
        FileOutputStream fos = new FileOutputStream(file);  
          
        byte[] buffer = new byte[8*1024];  
        int count = 0;  
        while ((count = is.read(buffer)) > 0) {   
        	//buffer[0]--;
            fos.write(buffer, 0, count);  
        }   
        fos.flush();  
        fos.close();  
        is.close();  
          
    }  
          
        return true;  
          
    }catch(Exception e){  
        e.printStackTrace();  
        return false;  
    }                 
        }  
	

	void openPdf()
		{
			  Uri path = Uri.parse("file:///data/data/com.example.soundtest/ww.pdf");
		       Intent intent = new Intent(Intent.ACTION_VIEW);
		       intent.setDataAndType(path, "application/pdf");
		       intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
		      // getApplicationContext().startActivity(intent);
		       
		       try {
                   startActivity(intent);
               } 
               catch (ActivityNotFoundException e) {
                   Toast.makeText(MainActivity.this, 
                       "No Application Available to View PDF", 
                       Toast.LENGTH_SHORT).show();
               }

		}


 类似资料: