-1

비동기 작업을 사용하는 것과 같은 여러 방법을 사용해 보았습니다. 기본 다운로드 관리자지만, 필자는 파일을 다운로드하는 것으로 끝났습니다 (필자는 requireddownload.pdf 파일 대신 php 파일을 다운로드한다고 생각합니다). 이는 필수 파일이 아닙니다. 그러나 다운로드 한 파일의 이름과 확장명은 requrieddownload.pdf입니다.URL의 종류가 http://myurl.com/file.php/...../requireddownload.pdf 인 PHP 구동 웹 사이트에서 pdf 파일을 다운로드하는 방법

미리 감사드립니다.

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 

AndroidManifest.xml에

에서

답변

0

첫 번째 단계를 선언 persmissions는 다운로더 클래스

public class Downloader { 

    public static void DownloadFile(String fileURL, File directory) { 
     try { 

      FileOutputStream f = new FileOutputStream(directory); 
      URL u = new URL(fileURL); 
      HttpURLConnection c = (HttpURLConnection) u.openConnection(); 
      c.setRequestMethod("GET"); 
      c.setDoOutput(true); 
      c.connect(); 

      InputStream in = c.getInputStream(); 

      byte[] buffer = new byte[1024]; 
      int len1 = 0; 
      while ((len1 = in.read(buffer)) > 0) { 
       f.write(buffer, 0, len1); 
      } 
      f.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 
} 
만들기

마지막으로 인터넷에서 PDF 파일을 다운로드 활동을 생성

public class PDFFromServerActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     String extStorageDirectory = Environment.getExternalStorageDirectory() 
     .toString(); 
     File folder = new File(extStorageDirectory, "pdf"); 
     folder.mkdir(); 
     File file = new File(folder, "Read.pdf"); 
     try { 
      file.createNewFile(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
     Downloader.DownloadFile("http://www.nmu.ac.in/ejournals/aspx/courselist.pdf", file); 

     showPdf(); 
    } 
    public void showPdf() 
     { 
      File file = new File(Environment.getExternalStorageDirectory()+"/pdf/Read.pdf"); 
      PackageManager packageManager = getPackageManager(); 
      Intent testIntent = new Intent(Intent.ACTION_VIEW); 
      testIntent.setType("application/pdf"); 
      List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY); 
      Intent intent = new Intent(); 
      intent.setAction(Intent.ACTION_VIEW); 
      Uri uri = Uri.fromFile(file); 
      intent.setDataAndType(uri, "application/pdf"); 
      startActivity(intent); 
     } 
}