2017-12-05 23 views
0

私のAndroidアプリケーションのアセットフォルダに格納されている "NEFT.pdf"ファイルを表示しようとしています。
次のコードは、このコードは、API 25以上で動作しないAPI 25fileProviderを使用してassetsフォルダからPDFファイルを開こうとしましたが、FileNotFoundExceptionが発生しました:ファイルやディレクトリがありません

private void CopyReadAssets(String filename) { 
    AssetManager assetManager = getAssets(); 
    InputStream in = null; 
    OutputStream out = null; 
    File file = new File(getFilesDir(), filename); 

    try { 
     in = assetManager.open(filename); 
     out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE); 

     copyFile(in, out); 
     in.close(); 
     in = null; 
     out.flush(); 
     out.close(); 
     out = null; 


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

     startActivity(intent); 
    } catch (Exception e) 
    { 
     Toast.makeText(PdfFilesList.this, "cra: "+e.toString(), Toast.LENGTH_SHORT).show(); 
    } 
} 

まで絶対に正常に動作します。エラーMODE_WORLD_READABLEはサポートされなくなりました。
私はMODE_PRIVATEにそれを変更したが、それは私にintent.getdataを通じてアプリから露出別のエラー

android.os.fileuriexposedexception()を与えます。
私はDeveloper.Android.comで説明されたコンセプトを適用しました。
これは私がエラーログに取得されるものです:

E/DisplayData:openFd:java.io.FileNotFoundException:そのようなファイルやディレクトリ

E/PdfLoaderは:ファイルを読み込むことができません(開きません)データを表示する[PDF:NEFT.pdf] + ContentOpenable、uri:content://com.user.plansmart.provider/pdf_files/NEFT.pdf

uriは私に似ています。誰でも私がここでエラーを見つけるのを助けることができます。

マニフェストファイルにプロバイダ要素があります。

<provider 
     android:name="android.support.v4.content.FileProvider" 
     android:authorities="${applicationId}.provider" 
     android:exported="false" 
     android:grantUriPermissions="true"> 
     <meta-data 
      android:name="android.support.FILE_PROVIDER_PATHS" 
      android:resource="@xml/provider_paths"/> 
    </provider> 

これはprovider_paths.xmlファイル

<?xml version="1.0" encoding="utf-8"?> 
<paths xmlns:android="http://schemas.android.com/apk/res/android"> 
<external-path name="external_files" path="."/> 
<files-path name="pdf_files" path="pdf/"/> 

これは、あなたがPDFを表示したい原因pdfファイル

private void displayFile(String filename) { 
    try { 
     File filePath = new File(getApplicationContext().getFilesDir(), "pdf"); 
     File newFile = new File(filePath, filename); 

     //new version 
     Uri fileUri = FileProvider.getUriForFile(PdfFilesList.this, 
       BuildConfig.APPLICATION_ID + ".provider", newFile); 

     getApplicationContext().grantUriPermission(PACKAGE_NAME, 
          fileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); 

     Intent intent = new Intent(Intent.ACTION_VIEW); 
     intent.setDataAndType(fileUri, "application/pdf"); 
     intent.setFlags(FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION); 
     startActivity(intent); 
    }catch (Exception e){ 
     e.printStackTrace(); 
     Toast.makeText(PdfFilesList.this, "df "+e.toString(), Toast.LENGTH_SHORT).show(); 
    } 
} 
+0

を使用しますそれらを提供するFileProviderクラス。再考する。 Recode。 – greenapps

+0

'getFilesDir()、" pdf ");'何って言ったの?資産?いいえ、資産ではありません。 getFilesDir()はプライベートな内部ストレージです。あなたの投稿を適応させてください。件名から始める。 – greenapps

+0

ファイルがアセットにありますか?次に、getFilesDir()を使用することはできません。あなたが持っているものを教えてください。 – greenapps

答えて

0

を表示するためのAndroidのコードです別のアプリケーション(Adobe Readerなど)に保存すると、次の操作を行って好むULD: - コピーファイルの場合

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

      InputStream in = null; 
      OutputStream out = null; 
      String state = Environment.getExternalStorageState(); 
      if (!Environment.MEDIA_MOUNTED.equals(state)) { 
       Toast.makeText(getActivity(), "External Storage is not Available", Toast.LENGTH_SHORT).show(); 
      } 
      File pdfDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDFs"); 
      if (!pdfDir.exists()) { 
       pdfDir.mkdir(); 
      } 
      File file = new File(pdfDir + "/abc.pdf"); 

      try 
      { 
       in = assetManager.open("abc.pdf"); 
       out = new BufferedOutputStream(new FileOutputStream(file)); 
       copyFile(in, out); 
       in.close(); 
       in = null; 
       out.flush(); 
       out.close(); 
       out = null; 
      } catch (Exception e) 
      { 
       Log.e("tag", e.getMessage()); 
      } 
      if (file.exists()) //Checking for the file is exist or not 
      { 
       Uri path = Uri.fromFile(file); 
       Intent objIntent = new Intent(Intent.ACTION_VIEW); 
       objIntent.setDataAndType(path, "application/pdf"); 
       objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       Intent intent1 = Intent.createChooser(objIntent, "Open PDF with.."); 
       try { 
        startActivity(intent1); 
       } catch (ActivityNotFoundException e) { 
        Toast.makeText(getActivity(), "Activity Not Found Exception ", Toast.LENGTH_SHORT).show(); 
       } 
      } else { 
       Toast.makeText(getActivity(), "The file not exists! ", Toast.LENGTH_SHORT).show(); 
      } 
     } 

デバイス・メモリに: -

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); 
     } 
    } 

あなたは資産内のファイルを持っている場合は、あなたが使用することはできません以下の権限

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

"ファイルが存在しません"。私はUriパスをこのUriパス= Uri.parse( "content://com.user.plansmart.provider/pdf_files/" + filename)に変更しようとしました。 – pamo

+0

もUri path = Uri.parse( "file:/// android_asset /" + filename)で試行しました。 do'nt work – pamo

+0

この答えにあなたの時間を費やしてはいけません。そのナンセンス。 – greenapps

関連する問題