2014-12-15 2 views
2

응용 프로그램 (내부 저장소)에 PDF 파일을 열려고합니다. 다음 adobe pdf가 열리면 다음 코드가 나타납니다. 문서 경로가 잘못되었습니다. "앱 내부에서 PDF 파일을 읽을 수만 있습니까? 주시기 바랍니다없는 경우 나 I 앱의 내부 저장 디렉토리에 미리응용 프로그램 내부에서 샘플 PDF 파일을 만들고 열고 엽니 다.

File file_source = new File(getApplicationContext().getFilesDir()+"/"+"sample.pdf"); 
       String string = "Hello world!"; 
       try 
       { 
        file_source.createNewFile(); 
        FileOutputStream outputStream; 
        outputStream = openFileOutput("sample.pdf", Context.MODE_PRIVATE); 
        outputStream.write(string.getBytes()); 
        outputStream.close(); 
        if(file_source.exists()) 
        { 
         Uri path = Uri.fromFile(file_source); 
         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(this, 
            "No Application Available to View PDF", 
            Toast.LENGTH_SHORT).show(); 
          Intent intent2 = new Intent(Intent.ACTION_VIEW); 
          startActivity(intent2); 


         } 
        } 
        else 
        { 
         Log.d("TAG","no file exists"); 

        } 
       } 
       catch (FileNotFoundException e) { 
        Log.d("TAG","File not found"); 
       } 
       catch (IOException ioe) { 
        Log.d("TAG","Exception while reading file" + ioe); 
       } 

답변

0

파일에 외부 storage.Thanks에 복사하여 응용 프로그램에 개인 기본적으로있는 방법을 알고있다. 즉, 어떤 PDF 리더 프로그램도 그 파일을 읽을 수 없다는 것을 의미합니다. 왜냐하면 당신의 애플 리케이션 pid로는 실행되지 않기 때문에 읽기 권한이 주어지지 않기 때문입니다.

Context.MODE_WORLD_READABLE 플래그를 사용하여 다른 응용 프로그램에 대한 명시적인 읽기 권한이있는 PDF를 저장해야합니다.

Context.openFileOutput() 및 Context.openFileInput()을 사용하여 내부 디렉토리의 파일을 읽고 씁니다. 이런 경로를 하드 코딩하지 마십시오. 경로가 변경 될 수 있습니다.

아래 코드를 사용하여 내부 저장소에서 외부 저장소로 파일을 복사 할 수 있습니다.

try { 
      String file_name="inputpdf.pdf"; 
      File tempfile = new File(directory, file_name); 
      FileInputStream  inStream = new FileInputStream(tempfile); 
      //where ctx is your context (this) 
      FileOutputStream fos = ctx.openFileOutput("Outputpdf", ctx.MODE_WORLD_WRITEABLE|ctx.MODE_WORLD_READABLE); 

      byte[] buffer = new byte[1024]; 

       int length; 
       //copy the file content in bytes 
       while ((length = inStream.read(buffer)) > 0){ 

        fos.write(buffer, 0, length); 

       } 

       inStream.close(); 
       fos.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
+0

안녕하세요 내가 pdf 파일은 항상 "문서가 비어"라고하는 following.But했다 .There는 출력 파일 extension.please이 무엇인지 외부 storage.Please 도움 – user3706655

+0

내부에서 복사 뭔가 잘못이다 .pdf가 없는지 확인한 다음 'Outputpdf'를 'Outputpdf.pdf'로 변경하십시오. –

+0

안녕하세요 Chandra 덕분에 pdf 파일을 열 수 있습니다. 문제점은 Environment.getExternalStorageDirectory가되어야하는 잘못된 "outputpdf"경로를 제공하고있었습니다. .... – user3706655

관련 문제