2017-03-03 58 views
0

我必須打開存儲在資產文件夾中的pdf文件。當我試圖在Android 6中打開文件時,它顯示錯誤。但是,其他Android版本沒有問題。我認爲它的許可問題。請幫助糾正這個錯誤。錯誤:無法訪問此文件檢查位置或網絡,然後再次嘗試Android 6

這裏是我的代碼 ....

dialBtn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 



      File fileBrochure = new File(Environment.getExternalStorageDirectory() + "/" + email+".pdf"); 
      if (!fileBrochure.exists()) 
      { 
       CopyAssetsbrochure(); 
      } 

      /** PDF reader code */ 
      File file = new File(Environment.getExternalStorageDirectory() + "/" + email+".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(getApplicationContext(), "Please install any pdf reader App.", 
         Toast.LENGTH_LONG).show(); 
       startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.adobe.reader"))); 
      } 
     } 
    }); 


} 

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(email+".pdf")) 
     { 
      InputStream in = null; 
      OutputStream out = null; 
      try 
      { 
       in = assetManager.open(files[i]); 
       out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/" + files[i]); 
       copyFile(in, out); 
       in.close(); 
       in = null; 
       out.flush(); 
       out.close(); 
       out = null; 
       break; 
      } 
      catch(Exception e) 
      { 
       Log.e("tag", e.getMessage()); 
      } 
     } 
    } 
} 

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

'無效CopyAssetsbrochure('。製作該'boolean CopyAssetsbrochure()'並在繼續執行代碼之前檢查返回值。 – greenapps

回答

1

您是否包括:在清單<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 和?

而且我發現這個代碼:

公共類SampleActivity延伸活動{

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

    } 

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

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

      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() + "/abc.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); 
     } 
    } 

} 

這裏是另一個答案的鏈接:)Read a pdf file from assets folder

+0

它不能正常工作。 – Shemil

+0

什麼樣的錯誤給你? –

+0

它與File的問題。 – Shemil

相關問題