2015-04-23 52 views
-1

我嘗試在我的應用程序中顯示.pdf文件。我無法顯示它。我得到無效的文件路徑錯誤與Adobe閱讀器和PDF查看器顯示文件無法打開。請你們中的任何一個讓我知道我在這裏犯了什麼錯誤。如果有更好的方法來實現這個,請教我。我已張貼我使用的代碼:無法在應用程序中打開.pdf文件

public class HelpScreen extends ActionBarActivity { 

    Toolbar toolbar; 

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

     // Initializing, setting text and color of tool bar 
     toolbar = (Toolbar) findViewById(R.id.appBar); 
     setSupportActionBar(toolbar); 
     toolbar.setTitleTextColor(Color.WHITE); 

     CopyReadAssets(); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_help_screen, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 


    private void CopyReadAssets() 
    { 
     AssetManager assetManager = getAssets(); 
     Log.d("Pana", "The value of assests is " +assetManager); 

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

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

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


     startActivity(intent); 
    } 

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

編輯的代碼:

public class HelpScreen extends ActionBarActivity { 

    Toolbar toolbar; 

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

     // Initializing, setting text and color of tool bar 
     toolbar = (Toolbar) findViewById(R.id.appBar); 
     setSupportActionBar(toolbar); 
     toolbar.setTitleTextColor(Color.WHITE); 

     CopyReadAssets(); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_help_screen, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 


    private void CopyReadAssets() { 
     AssetManager assetManager = getAssets(); 
     Log.d("Pana", "The value of assests is " + assetManager); 

     InputStream in = null; 
     OutputStream out = null; 
     File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "help_document_task_management_system_document_4.pdf"); 
     // File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "help_document_task_management_system_document_4.pdf"); 
     try { 
      in = assetManager.open("help_document_task_management_system_document_4.pdf"); 
      out = openFileOutput(file.getName(), Context.MODE_PRIVATE); 

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

     Intent intent = new Intent(Intent.ACTION_VIEW); 
     intent.setDataAndType(
       Uri.fromFile(file), "application/pdf"); 
     startActivity(intent); 
    } 

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

請讓我知道我的錯誤,並幫助我來的這一點。提前致謝。

+0

你從Uri.parse得到了什麼字符串? – Thealon

+0

@Thealon:我得到的字符串的值是:Intent {act = android.intent.action.VIEW dat = file:///data/data/com.ms.t.tms/files/help_document_task_management_system_document_4.pdf typ = application/pdf} – Keshav1234

回答

2

第三方應用程序無權訪問該文件。使用FileProvider來提供它,或將文件複製到external storage而不是internal storage

+0

正如你所說,我在我的文件語句中使用Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),你能讓我知道我應該怎麼做Uri.Parse行來獲取這個文件。提前致謝。 – Keshav1234

+0

@ Keshav1234:不要使用'Uri.parse()'。使用'Uri.fromFile()',傳入用於下載文件的相同'File'對象。 – CommonsWare

+0

我這樣做,我得到一個錯誤,指出文件無法訪問,我已經更新了更改後的代碼,你能看看,請讓我知道我應該改變什麼?我的pdf文件在資產文件夾中。提前致謝。 – Keshav1234

0

看起來您的文件路徑中有兩個/,在getFilesDir()之後創建。

相關問題