2014-08-28 2 views
1

여기에서 내 자산 폴더에서 .pdf 파일을 읽으려고하는데 "이 문서를 열 수 없습니다."라는 오류가 표시됩니다. 나는 .pdf 파일을 SD 카드에 복사 한 다음 거기에서 읽으려고했지만 성공하지 못했습니다. 여기에 코드가 있습니다. 도와주세요.프로젝트의 원시 폴더에서 .pdf 읽기

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    File fileBrochure = new File("/sdcard/fleetman.pdf"); 
    if (!fileBrochure.exists()) 
    { 
     CopyAssetsbrochure(); 
    } 

    /** PDF reader code */ 
    File file = new File("/sdcard/fleetman.pdf");   

    Intent intent = new Intent(Intent.ACTION_VIEW); 
    intent.setDataAndType(Uri.fromFile(file),"application/pdf"); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    try 
    { 
     getApplicationContext().startActivity(intent); 
    } 
    catch (ActivityNotFoundException e) 
    { 
     Toast.makeText(MainActivity.this, "NO Pdf Viewer", Toast.LENGTH_SHORT).show(); 
    } 
} 

//method to write the PDFs file to sd card 
    private void CopyAssetsbrochure() { 
     AssetManager assetManager = getAssets(); 
     String[] files = null; 
     try 
     { 
      files = assetManager.list(""); 
     } 
     catch (IOException e) 
     { 
      Log.e("tag", e.getMessage()); 
     } 
     for(int i=0; i<files.length; i++) 
     { 
      String fStr = files[i]; 
      if(fStr.equalsIgnoreCase("fleetman.pdf")) 
      { 
       InputStream in = null; 
       OutputStream out = null; 
       try 
       { 
        in = assetManager.open(files[i]); 
        out = new FileOutputStream("/sdcard/" + files[i]); 
        copyFile(in, out); 
        in.close(); 
        in = null; 
        out.flush(); 
        out.close(); 
        out = null; 
        break; 
       } 
       catch(Exception e) 
       { 
        Log.e("tag", e.getMessage()); 
       } 
      } 
     } 
    } 

답변

5
try this, hope it ll help you 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     CopyReadPDFFromAssets(); 

    } 

    private void CopyReadPDFFromAssets() 
    { 
     AssetManager assetManager = getAssets(); 

     InputStream in = null; 
     OutputStream out = null; 
     File file = new File(getFilesDir(), "pdfdemofile.pdf"); 
     try 
     { 
      in = assetManager.open("pdfdemofile.pdf"); 
      out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE); 

      copyPdfFile(in, out); 
      in.close(); 
      in = null; 
      out.flush(); 
      out.close(); 
      out = null; 
     } catch (Exception e) 
     { 
      Log.e("exception", e.getMessage()); 
     } 

     Intent intent = new Intent(Intent.ACTION_VIEW); 
     intent.setDataAndType(
       Uri.parse("file://" + getFilesDir() + "/pdfdemofile.pdf"), 
       "application/pdf"); 

     startActivity(intent); 
    } 

    private void copyPdfFile(InputStream in, OutputStream out) throws IOException 
    { 
     byte[] buffer = new byte[1024]; 
     int read; 
     while ((read = in.read(buffer)) != -1) 
     { 
      out.write(buffer, 0, read); 
     } 
    } 

이 매니페스트 파일에 추가 작동

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
+0

... Thanku .. :) –

+0

@gautamjoshi는 저를 upvote에 적어주세요 : P –

+0

나는 10 UpVotes 당신에게 주어진 것입니다. 그러나 등급 문제. 이메일 주소를 알려줘. 내가 어떤 문제에 빠지면 당신에게 상담 할 것이다. : P;) –

관련 문제