2017-06-21 17 views
0

PDFファイルを受信できるアプリケーションで作業しています。このアプリは、現在、受信したファイルを内部のアプリケーションディレクトリにbyte []として保存し、そのローカルパスにアクセスできます。Android:ファイルパスを使用してPDFをエクスポート

このデータを外部メモリに保存する前に、そのデータをPDFファイルに変換したいと思っています。

私は以下のコードを使用してこれを行うことができますが、私はそれにアクセスしようとすると無効な形式であると言われます。任意のアイデアをどのようにこれを修正するには?このコードではなく、ステップバイステップどの

// ---------- EXPORT IMAGE TASK ---------- 
private class ExportPDF extends AsyncTask<Void, Void, String> { 
    @Override 
    protected String doInBackground(Void... voids) { 
     String pathToExternalStorage = Environment.getExternalStorageDirectory().toString(); 
     File appDirectory = new File(pathToExternalStorage + "/" + getString(R.string.app_name)); 
     if (!appDirectory.exists()) { 
      appDirectory.mkdirs(); 
     } 

     File imageFile = new File(appDirectory.getAbsolutePath() + "/PDF_" + filename.hashCode() + ".pdf"); 
     if (!imageFile.exists()) { 
      try { 
       FileOutputStream fos = new FileOutputStream(imageFile.getPath()); 
       fos.write(new File(localPath).toString().getBytes()); 
       fos.close(); 
      } catch (FileNotFoundException e) { 
       Log.e(TAG, e.toString()); 
      } catch (IOException e) { 
       Log.e(TAG, e.toString()); 
      } 
     } 
     return imageFile.getAbsolutePath(); 
    } 

    @Override 
    protected void onPostExecute(String aString) { 
     exportPDF(aString); 
    } 
} 

private void exportPDF(String filePath) { 
    Uri imageUri = Uri.parse(filePath); 
    Intent sharingIntent = new Intent(Intent.ACTION_VIEW); 
    sharingIntent.setDataAndType(imageUri, "application/pdf"); 
    startActivity(sharingIntent); 
} 

答えて

0
fos.write(new File(localPath).toString().getBytes()); 

  • が何らかの値に基づいFileオブジェクトを作成(new File(localPath)
  • そのファイルへのパスの文字列表現を作成します( new File(localPath).toString()
  • そのファイルへのパスの文字列表現のbyte[]を作成します(new File(localPath).toString().getBytes()
  • byte[]

FileOutputStreamの結果はimageFileによって識別されたファイルが他のファイルへのパスが含まれていることであることを書き込みます。これは有効なPDFではありません。

私の推測では、localPathの内容をimageFileにコピーしようとしていると思います。このコードはそれをしません。

FileProviderを使用すると、データの2番目のコピーを作成するのではなく、localPathから直接PDFビューアにPDFを提供することができます。

関連する問題