【发布时间】:2017-04-10 06:23:41
【问题描述】:
我正在尝试在 gmail 应用程序中附加 pdf 文件。我已阅读this 和this(应用解决方案)我正在尝试;
public static void attachFile(Context ctx) {
String TAG = "Attach";
File documentsPath = new File(ctx.getFilesDir(), "documents");
Log.i(TAG,"documentsAbsolutePath Output");
Log.i(TAG, documentsPath.getAbsolutePath().toString());
File file = new File(documentsPath, "sample.pdf");
if ( file.exists() ) {
Toast.makeText(ctx, "Exits", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(ctx, "Not Exist", Toast.LENGTH_LONG).show();
}
Log.i(TAG,"file Output");
Log.i(TAG, file.toString());
Log.i(TAG, String.valueOf(file.length()));
Uri uri = FileProvider.getUriForFile(ctx, "com.example.fyp_awais.attachfiletest2.fileprovider", file);
Log.i(TAG,"URI Output");
Log.i(TAG,uri.toString());
Intent intent = ShareCompat.IntentBuilder.from((Activity) ctx)
.setType("application/pdf")
.setStream(uri)
.setChooserTitle("Choose bar")
.createChooserIntent()
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
ctx.startActivity(intent);
}
输出
documentsAbsolutePath Output
/data/data/com.example.fyp_awais.attachfiletest2/files/documents
file Output
/data/data/com.example.fyp_awais.attachfiletest2/files/documents/sample.pdf
0
URI Output
content://com.example.fyp_awais.attachfiletest2.fileprovider/pdf_folder/sample.pdf
菜单
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.fyp_awais.attachfiletest2.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepath" />
</provider>
文件路径.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="pdf_folder" path="documents/"/>
</paths>
</PreferenceScreen
一个pdf文件保存在Galaxy Core Prime\Phone\documents中。 (文件大小:53.7KB)
但它给了
无法附加空文件。
我对@987654333@ 这一行中的文件夹名称感到困惑。该文件位于\Phone\documents 中。那么为什么是文件夹名称?
编辑 1
尝试将setType(application/pdf) 替换为setType("message/rfc822"),但没有成功。有什么帮助吗?
【问题讨论】:
-
尝试用这个
setType("message/rfc822")替换setType -
@FAЯAƸ 试过了。它不起作用。
-
@FAЯAƸ 我正在尝试使用
FileProvider来执行此操作。用于与其他应用共享文件。 -
File file = new File(documentsPath, "sample.pdf");。之后添加以下代码:if ( ! file.exists() ) {Toast ( ... file.. file.getAbsolutePath() ... not found...); return;}。请告诉结果。