2017-12-03 24 views
0

私のアプリの焦点は:PDFファイルを作成し、Whatsapp、Messenger、Gmailなどで共有しています...私はファイルを共有しようとしています(それは/storage/emulated/0/に保存されています)。しかし、私はトーストで "空のファイルを添付できません"というエラーを返します。共有の方法のコードは、以下である:私はストレージからファイルを共有できません - "空のファイルを添付できません"

MainActivity.java

Intent i = new Intent(Intent.ACTION_SEND); 
i.setType("*/*"); 
i.putExtra(Intent.EXTRA_TEXT, "NOME: ".concat(String.valueOf(Hawk.get("register_name")))); 
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); 
i.putExtra(Intent.EXTRA_SUBJECT, String.valueOf(Hawk.get("register_name")).concat(" Business Card")); 
i.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(this, "test.com.br.businesscard.provider", new File(getExternalFilesDir(null), "business.pdf"))); 
startActivity(Intent.createChooser(i, "Sending e-mail...")); 

ファイル/storage/emulated/0に直接ためprovider_paths.xml

<paths> 
    <external-path 
     name="external_files" 
     path="." /> 
</paths> 
+0

「/ storage/emulated/0 /」に保存されています。 'getExternalFilesDir(null)'は、外部記憶装置内の特定の場所を指します。 – CommonsWare

+0

どうすればこの問題を解決できますか? ** getExternalFilesDir **のパラメータで場所を渡しますか?次のように:_new File(getExternalFilesDir( "/ storage/emulated/0 /")_、動作しないため:( –

+1

"どうすればこの問題を解決できますか?" - 文字通り*ファイルが入っていることを意味しますか? '/ storage/emulated/0'?もしそうなら、' new File(Environment.getExternalStorageDirectory()、 "business.pdf") 'を使って' File'を取得してください。 – CommonsWare

答えて

2

、を建設する方法は:...ファイルのファイル名である

new File(Environment.getExternalStorageDirectory(), ...) 

(例えば、"business.pdf")。

getExternalFilesDir(null)は、/storage/emulated/0/Android/data/.../のような場所のファイル用です。ここで、...はアプリケーションIDです。

あなたのFileProvider設定はどちらの場所にも適用されます。ただし、FileProviderは存在しないファイルを指すFileオブジェクトを使用してコンテンツを配信することができないため、問題が発生しました。

関連する問題