Guest User

Untitled

a guest
Sep 28th, 2017
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1. public class Visualizador_pdf extends AppCompatActivity {
  2.  
  3.     private TextView textView;
  4.     private File file;
  5.  
  6.     //Declaracions de la conexió:
  7.  
  8.     private static final String TAG = "Visualizador_pdf";
  9.     public static Context cntx = null;
  10.     private static final String HOST = "ftp.appdesigndm.com";
  11.     private static final String USER = "master@appdesigndm.com";
  12.     private static final String PASS = "Alin1022";
  13.     public static FTPClient mFTPClient = null;
  14.  
  15.     String srcFilePath = "";
  16.     String desFileStream = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/PDF/" + "des.pdf";
  17.  
  18.  
  19.  
  20.     //-------------------------------------------------------------------------------------------------------------------------------
  21.  
  22.     @Override
  23.     protected void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.activity_visualizador_pdf);
  26.  
  27.         cntx = this;
  28.         mFTPClient = new FTPClient();
  29.  
  30.         //String ruta = "";
  31.         //file = new File(ruta);
  32.         //textView = (TextView) findViewById(R.id.text);
  33.  
  34.         Intent intent = getIntent();
  35.         Bundle extras = intent.getExtras();
  36.  
  37.         if (extras != null) {
  38.  
  39.             srcFilePath = extras.getString("PDF");
  40.  
  41.             Toast.makeText(getApplicationContext(), srcFilePath, Toast.LENGTH_LONG).show();
  42.  
  43.  
  44.             HiloDescarga hilo = new HiloDescarga();
  45.             hilo.execute(srcFilePath, desFileStream);
  46.  
  47.             try {
  48.                 Thread.sleep(1000);
  49.             } catch (InterruptedException e) {
  50.                 e.printStackTrace();
  51.             }
  52.  
  53.             //llamarpdf();
  54.  
  55.         }
  56.  
  57.     }
  58.  
  59.     public static boolean download(String srcFilePath, String desFilePath) {
  60.         boolean status = false;
  61.  
  62.         try {
  63.             File f = new File(desFilePath.substring(0, desFilePath.lastIndexOf("/") + 1));
  64.             if (!f.exists()) {
  65.                 f.mkdirs();
  66.             }
  67.  
  68.             FileOutputStream desFileStream = new FileOutputStream(desFilePath);
  69.  
  70.             mFTPClient.connect(HOST);
  71.             mFTPClient.login(USER, PASS);
  72.  
  73.             status = mFTPClient.retrieveFile(srcFilePath, desFileStream);
  74.             desFileStream.close();
  75.             return status;
  76.  
  77.         } catch (Exception e) {
  78.             Log.e(TAG, e.toString());
  79.             Log.d(TAG, "download failed");
  80.         } finally {
  81.             try {
  82.                 mFTPClient.disconnect();
  83.             } catch (Exception e) {
  84.                 Log.e(TAG, e.toString());
  85.             }
  86.         }
  87.  
  88.         return status;
  89.  
  90.     }
  91.  
  92.    
  93.  
  94.  
  95.  
  96.     public void llamarpdf() {
  97.  
  98.         String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/PDF/" + "des.pdf";
  99.  
  100.         File file = new File(path);
  101.         Intent aviso = new Intent(Intent.ACTION_VIEW);
  102.         aviso.setDataAndType(Uri.fromFile(file), "application/pdf");
  103.         aviso.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
  104.         Intent intent = Intent.createChooser(aviso, "Open file");
  105.         try {
  106.             startActivity(intent);
  107.         } catch (ActivityNotFoundException e) {
  108.  
  109.         }
  110.         finish();
  111.     }
  112.     @Override
  113.     protected void onStop() {
  114.         super.onStop();
  115.     }
  116. }
Add Comment
Please, Sign In to add comment